feat: add request work support
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 5s
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 5s
This commit is contained in:
parent
c5d250ab0c
commit
c6c187b8d3
3 changed files with 127 additions and 4 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import barcode from "barcode";
|
||||
import createReport from "docx-templates";
|
||||
import ThaiBahtText from "thai-baht-text";
|
||||
import { District, Province, SubDistrict } from "@prisma/client";
|
||||
|
|
@ -71,19 +72,52 @@ const quotationData = (id: string) =>
|
|||
},
|
||||
});
|
||||
|
||||
const requestWorkData = (id: string, step?: number) =>
|
||||
prisma.requestWork.findFirst({
|
||||
where: { id },
|
||||
include: {
|
||||
processByUser: true,
|
||||
productService: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
},
|
||||
request: {
|
||||
include: {
|
||||
employee: {
|
||||
include: {
|
||||
subDistrict: true,
|
||||
district: true,
|
||||
province: true,
|
||||
},
|
||||
},
|
||||
quotation: true,
|
||||
},
|
||||
},
|
||||
stepStatus: {
|
||||
where: { step },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@Route("api/v1/doc-template")
|
||||
@Tags("Document Template")
|
||||
export class DocTemplateController extends Controller {
|
||||
@Get()
|
||||
async getTemplate() {
|
||||
async getTemplate(@Query() templateGroup?: string) {
|
||||
if (
|
||||
process.env.DOCUMENT_TEMPLATE_PROVIDER &&
|
||||
process.env.DOCUMENT_TEMPLATE_PROVIDER === "edm-api"
|
||||
) {
|
||||
const ret = await edmList("file", DOCUMENT_PATH);
|
||||
const ret = await edmList(
|
||||
"file",
|
||||
templateGroup ? [templateGroup, ...DOCUMENT_PATH] : DOCUMENT_PATH,
|
||||
);
|
||||
if (ret) return ret.map((v) => v.fileName);
|
||||
}
|
||||
return await listFile(DOCUMENT_PATH.join("/") + "/");
|
||||
return await listFile(
|
||||
(templateGroup ? [templateGroup, ...DOCUMENT_PATH] : DOCUMENT_PATH).join("/") + "/",
|
||||
);
|
||||
}
|
||||
|
||||
@Get("{documentTemplate}")
|
||||
|
|
@ -92,6 +126,7 @@ export class DocTemplateController extends Controller {
|
|||
@Query() data: string,
|
||||
@Query() dataId: string,
|
||||
@Query() dataOnly?: boolean,
|
||||
@Query() templateGroup?: string,
|
||||
): Promise<Readable | Record<string, any>> {
|
||||
let record: Record<string, any>;
|
||||
|
||||
|
|
@ -132,12 +167,19 @@ export class DocTemplateController extends Controller {
|
|||
}),
|
||||
);
|
||||
break;
|
||||
case "request-work":
|
||||
record = await requestWorkData(dataId).then((requestWork) => ({
|
||||
request: replaceEmptyField(requestWork?.request),
|
||||
requestWork: replaceEmptyField(requestWork),
|
||||
employee: requestWork?.request.employee,
|
||||
}));
|
||||
default:
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "No data for template", "noDataTemplate");
|
||||
}
|
||||
|
||||
if (!data) throw notFoundError("Data");
|
||||
if (dataOnly) return record;
|
||||
if (templateGroup) documentTemplate = templateGroup + "/" + documentTemplate;
|
||||
|
||||
let template: Buffer<ArrayBufferLike> | null = null;
|
||||
|
||||
|
|
@ -210,6 +252,13 @@ export class DocTemplateController extends Controller {
|
|||
thaiBahtText: (input: string | number) => {
|
||||
ThaiBahtText(typeof input === "string" ? input.replaceAll(",", "") : input);
|
||||
},
|
||||
barcode: async (data: string) =>
|
||||
new Promise<string>((resolve, reject) =>
|
||||
barcode("code39", { data, width: 400, height: 100 }).getBase64((err, data) => {
|
||||
if (!err) return resolve(data);
|
||||
return reject(err);
|
||||
}),
|
||||
),
|
||||
},
|
||||
}).then(Buffer.from);
|
||||
|
||||
|
|
@ -218,7 +267,11 @@ export class DocTemplateController extends Controller {
|
|||
}
|
||||
|
||||
function replaceEmptyField<T>(data: T): T {
|
||||
return JSON.parse(JSON.stringify(data).replace(/null|\"\"/g, '"\-"'));
|
||||
try {
|
||||
return JSON.parse(JSON.stringify(data).replace(/null|\"\"/g, '"\-"'));
|
||||
} catch (e) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
type FullAddress = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue