From 41c27dced5f7dd83bf480976c46764670488edeb Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 5 Apr 2024 10:53:52 +0700 Subject: [PATCH] feat: customer create endpoint --- src/controllers/customer-controller.ts | 63 ++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/controllers/customer-controller.ts diff --git a/src/controllers/customer-controller.ts b/src/controllers/customer-controller.ts new file mode 100644 index 0000000..97f3625 --- /dev/null +++ b/src/controllers/customer-controller.ts @@ -0,0 +1,63 @@ +import { Prisma, Status } from "@prisma/client"; +import { Body, Controller, Delete, Get, Path, Post, Put, Query, Request, Route, Tags } from "tsoa"; +import { RequestWithUser } from "../interfaces/user"; +import prisma from "../db"; +import minio from "../services/minio"; +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; + +if (!process.env.MINIO_BUCKET) { + throw Error("Require MinIO bucket."); +} + +const MINIO_BUCKET = process.env.MINIO_BUCKET; + +export type CustomerCreate = { + code?: string; + status?: Status; + customerType: string; + customerName: string; + customerNameEN: string; +}; + +export type CustomerUpdate = { + code?: string; + status?: "ACTIVE" | "INACTIVE"; + customerType?: string; + customerName?: string; + customerNameEN?: string; +}; + +function imageLocation(id: string) { + return `customer/img-${id}`; +} + +@Route("api/customer") +@Tags("Customer") +export class CustomerController extends Controller { + @Post() + async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) { + const record = await prisma.customer.create({ + data: { + ...body, + createdBy: req.user.name, + updateBy: req.user.name, + }, + }); + + this.setStatus(HttpStatus.CREATED); + + return Object.assign(record, { + imageUrl: await minio.presignedGetObject( + MINIO_BUCKET, + imageLocation(record.id), + 12 * 60 * 60, + ), + imageUploadUrl: await minio.presignedPutObject( + MINIO_BUCKET, + imageLocation(record.id), + 12 * 60 * 60, + ), + }); + } +}