feat: auto generate customer code
This commit is contained in:
parent
de01db361a
commit
ac58fe47e9
1 changed files with 8 additions and 1 deletions
|
|
@ -96,10 +96,17 @@ export class CustomerController extends Controller {
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) {
|
async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) {
|
||||||
|
const last = await prisma.customer.findFirst({
|
||||||
|
orderBy: { createdAt: "desc" },
|
||||||
|
where: { customerType: body.customerType },
|
||||||
|
});
|
||||||
|
|
||||||
|
const code = `${body.customerType}${(+(last?.code.slice(-6) || 0) + 1).toString().padStart(6, "0")}`;
|
||||||
|
|
||||||
const record = await prisma.customer.create({
|
const record = await prisma.customer.create({
|
||||||
data: {
|
data: {
|
||||||
...body,
|
...body,
|
||||||
code: "CUSTOMER001",
|
code,
|
||||||
createdBy: req.user.name,
|
createdBy: req.user.name,
|
||||||
updateBy: req.user.name,
|
updateBy: req.user.name,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue