feat: add attachment endpoints for credit note

This commit is contained in:
Methapon2001 2025-01-10 09:05:03 +07:00
parent 5c3dc9abcd
commit a3735abb78
2 changed files with 66 additions and 0 deletions

View file

@ -3,6 +3,7 @@ import {
Controller,
Delete,
Get,
Head,
Path,
Post,
Put,
@ -23,6 +24,7 @@ import {
} from "../services/permission";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { deleteFile, fileLocation, getFile, getPresigned, listFile, setFile } from "../utils/minio";
import { notFoundError } from "../utils/error";
import { CreditNotePaybackType, CreditNoteStatus, Prisma } from "@prisma/client";
import { queryOrNot } from "../utils/relation";
@ -548,3 +550,64 @@ export class CreditNoteActionController extends Controller {
});
}
}
@Route("api/v1/credit-note/{creditNoteId}")
@Tags("Credit Note")
export class CreditNoteAttachmentController extends Controller {
async #checkPermission(user: RequestWithUser["user"], id: string) {
const creditNoteData = await prisma.creditNote.findFirst({
where: { id },
include: {
requestWork: true,
quotation: {
include: {
registeredBranch: { include: branchRelationPermInclude(user) },
},
},
},
});
if (!creditNoteData) throw notFoundError("Credit Note");
await permissionCheck(user, creditNoteData.quotation.registeredBranch);
return creditNoteData;
}
@Get("attachment")
@Security("keycloak")
async listAttachment(@Request() req: RequestWithUser, @Path() creditNoteId: string) {
await this.#checkPermission(req.user, creditNoteId);
return await listFile(fileLocation.creditNote.attachment(creditNoteId));
}
@Get("attachment/{name}")
@Security("keycloak")
async getAttachment(@Path() creditNoteId: string, @Path() name: string) {
return await getFile(fileLocation.creditNote.attachment(creditNoteId, name));
}
@Head("attachment/{name}")
async headAttachment(@Path() creditNoteId: string, @Path() name: string) {
return await getPresigned("head", fileLocation.creditNote.attachment(creditNoteId, name));
}
@Put("attachment/{name}")
@Security("keycloak")
async putAttachment(
@Request() req: RequestWithUser,
@Path() creditNoteId: string,
@Path() name: string,
) {
await this.#checkPermission(req.user, creditNoteId);
return await setFile(fileLocation.creditNote.attachment(creditNoteId, name));
}
@Delete("attachment/{name}")
@Security("keycloak")
async delAttachment(
@Request() req: RequestWithUser,
@Path() creditNoteId: string,
@Path() name: string,
) {
await this.#checkPermission(req.user, creditNoteId);
return await deleteFile(fileLocation.creditNote.attachment(creditNoteId, name));
}
}

View file

@ -112,4 +112,7 @@ export const fileLocation = {
task: {
attachment: (taskId: string, name?: string) => `task/attachment-${taskId}/${name || ""}`,
},
creditNote: {
attachment: (taskId: string, name?: string) => `credit-note/attachment-${taskId}/${name || ""}`,
},
};