Files
HKSingleParty/03_source/cms_backend/prisma/seeds/invoiceItem.ts

77 lines
2.3 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import { _mock } from './_mock';
import { _tags } from './assets';
import { _addressBooks } from './_others';
import { fSub, fAdd } from './utils/format-time';
const prisma = new PrismaClient();
export const INVOICE_SERVICE_OPTIONS = Array.from({ length: 8 }, (_, index) => ({
id: _mock.id(index),
name: _tags[index],
price: _mock.number.price(index),
}));
const ITEMS = Array.from({ length: 3 }, (__, index) => {
const total = INVOICE_SERVICE_OPTIONS[index].price * _mock.number.nativeS(index);
return {
id: _mock.id(index),
total,
title: _mock.productName(index),
description: _mock.sentence(index),
price: INVOICE_SERVICE_OPTIONS[index].price,
service: INVOICE_SERVICE_OPTIONS[index].name,
quantity: _mock.number.nativeS(index),
};
});
async function invoiceItem() {
await prisma.orderItem.deleteMany({});
for (let index = 1; index < 3 + 1; index++) {
const shipping = 10;
const discount = 10;
const taxes = 10;
const items = (index % 2 && ITEMS.slice(0, 1)) || (index % 3 && ITEMS.slice(1, 3)) || ITEMS;
const subtotal = items.reduce((accumulator, item) => accumulator + item.price * item.quantity, 0);
const totalAmount = subtotal - shipping - discount + taxes;
const temp = await prisma.invoiceItem.upsert({
where: { id: index.toString() },
update: {},
create: {
id: index.toString(),
taxes,
status: (index % 2 && 'paid') || (index % 3 && 'pending') || (index % 4 && 'overdue') || 'draft',
discount,
shipping,
subtotal: items.reduce((accumulator, item) => accumulator + item.price * item.quantity, 0),
totalAmount,
items,
invoiceNumber: `INV-199${index}`,
invoiceFrom: _addressBooks[index],
invoiceTo: _addressBooks[index + 1],
sent: _mock.number.nativeS(index),
dueDate: new Date(fAdd({ days: index + 15, hours: index })),
createDate: new Date(fAdd({ days: index + 15, hours: index })),
},
});
}
console.log('seed invoiceItem done');
}
const invoiceItemSeed = invoiceItem()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
export { invoiceItemSeed };