27 lines
805 B
TypeScript
27 lines
805 B
TypeScript
import type { HttpContext } from '@adonisjs/core/http'
|
|
import ImportService from '#services/import_service'
|
|
|
|
export default class ImportsController {
|
|
/**
|
|
* POST /api/imports/products (multipart, field "file")
|
|
* Excel gồm cột: sku, condition, qty, price
|
|
*/
|
|
async products({ request, response, auth }: HttpContext) {
|
|
const file = request.file('file', {
|
|
size: '20mb',
|
|
extnames: ['xlsx', 'xls', 'csv'],
|
|
})
|
|
|
|
if (!file) {
|
|
return response.badRequest({ error: 'Thiếu file (field "file")' })
|
|
}
|
|
if (!file.isValid) {
|
|
return response.badRequest({ error: file.errors })
|
|
}
|
|
|
|
// file.tmpPath có sẵn khi autoProcess=true
|
|
const summary = await ImportService.importFromFile(file.tmpPath!, auth.getUserOrFail().username)
|
|
return summary
|
|
}
|
|
}
|