41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import Log from '#models/log'
|
|
|
|
export type LogAction = 'create' | 'update' | 'delete' | 'import' | 'sync' | 'suggest'
|
|
|
|
interface RecordLogInput {
|
|
username: string
|
|
actionName: string
|
|
action: LogAction
|
|
productId?: number | null
|
|
meta?: Record<string, any> | null
|
|
}
|
|
|
|
/**
|
|
* Ghi nhật ký thao tác CUD của người dùng lên sản phẩm.
|
|
*/
|
|
export default class LogService {
|
|
static async record(input: RecordLogInput): Promise<Log> {
|
|
return Log.create({
|
|
username: input.username,
|
|
actionName: input.actionName,
|
|
action: input.action,
|
|
productId: input.productId ?? null,
|
|
meta: input.meta ?? null,
|
|
})
|
|
}
|
|
|
|
/** Ghi log hàng loạt (vd import nhiều dòng). */
|
|
static async recordMany(inputs: RecordLogInput[]): Promise<void> {
|
|
if (!inputs.length) return
|
|
await Log.createMany(
|
|
inputs.map((i) => ({
|
|
username: i.username,
|
|
actionName: i.actionName,
|
|
action: i.action,
|
|
productId: i.productId ?? null,
|
|
meta: i.meta ?? null,
|
|
}))
|
|
)
|
|
}
|
|
}
|