33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
import type { HttpContext } from '@adonisjs/core/http'
|
|
import vine from '@vinejs/vine'
|
|
import PricingService from '#services/pricing_service'
|
|
import { enqueuePricingBatch } from '#services/queue_service'
|
|
|
|
const approveValidator = vine.compile(
|
|
vine.object({ price: vine.number().min(0).optional() })
|
|
)
|
|
|
|
export default class PricingController {
|
|
/** POST /api/pricing/suggest/:id (on-demand, đồng bộ) */
|
|
async suggest({ params, auth }: HttpContext) {
|
|
return PricingService.suggestForProduct(Number(params.id), auth.getUserOrFail().username)
|
|
}
|
|
|
|
/** POST /api/pricing/suggest/:id/approve (duyệt & áp giá khi vượt ngưỡng) */
|
|
async approve({ params, request, auth }: HttpContext) {
|
|
const { price } = await request.validateUsing(approveValidator)
|
|
return PricingService.approve(Number(params.id), auth.getUserOrFail().username, price)
|
|
}
|
|
|
|
/**
|
|
* POST /api/pricing/batch (chạy hàng loạt qua queue)
|
|
* body: { productIds?: number[] } — bỏ trống = toàn bộ
|
|
*/
|
|
async batch({ request, auth }: HttpContext) {
|
|
const productIds: number[] =
|
|
request.input('productIds') ?? (await PricingService.productIdsForBatch())
|
|
await enqueuePricingBatch(productIds, auth.getUserOrFail().username)
|
|
return { enqueued: productIds.length }
|
|
}
|
|
}
|