Skip to content

Commit

Permalink
fix: make admin can use borrowing key features and split function sen…
Browse files Browse the repository at this point in the history
…dLineNotification function
  • Loading branch information
Bunyawat Naunnak committed Aug 14, 2024
1 parent f51c660 commit 19eb282
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 35 deletions.
6 changes: 3 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
pnpm run format
pnpm run check
pnpm run lint
bun run format
bun run check
bun run lint
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smovidyaparcel",
"version": "1.0.3",
"version": "1.2.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ model Parcel_Project {
project_id String
student_id String
description_admin String @default("")
image_url String @default("")
amount Int @default(0)
description String @default("")
status BORROWING_STATUS @default(PENDING)
Expand Down
54 changes: 25 additions & 29 deletions src/server/api/routers/parcel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { z } from "zod";
import { TRPCError } from "@trpc/server";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import {
LINE_TOKEN,
ParcelDepartmentSchema,
ParcelGroupSchema,
ParcelTypeSchema,
} from "~/utils/constant";
import { BORROWING_STATUS, PARCEL_TYPE } from "@prisma/client";
import { sendMessage } from "~/utils/function";

/**
* TRPC Router for handling parcel-related operations.
Expand Down Expand Up @@ -44,27 +44,36 @@ export const parcelRouter = createTRPCRouter({
}),
)
.query(async ({ ctx, input }) => {
const { student_id: studentId, project_id: projectId } = input;
const key_project = "0000000000";

// Check if the student is in the project with ID `0000000000`.
const isStudentInProject = await ctx.db.project_Student.findFirst({
const is_student_in_project = await ctx.db.project_Student.findFirst({
where: {
project_id: key_project,
student_id: studentId,
student_id: input.student_id,
},
});

// Check if the student is Admin
const is_student_admin = await ctx.db.student.findFirst({
where: {
student_id: input.student_id,
isAdmin: true,
},
});

return await ctx.db.parcel.findMany({
where:
isStudentInProject && projectId === key_project
is_student_in_project && input.project_id === key_project
? { available: true, type: PARCEL_TYPE.KEY }
: {
available: true,
NOT: {
type: "KEY",
: is_student_admin && input.project_id === key_project
? { available: true, type: PARCEL_TYPE.KEY }
: {
available: true,
NOT: {
type: PARCEL_TYPE.KEY,
},
},
},
});
}),

Expand Down Expand Up @@ -164,26 +173,13 @@ export const parcelRouter = createTRPCRouter({
amount: amount - input.amount,
},
});
const student_name = await tx.student.findFirst({
const student = await tx.student.findFirst({
where: { student_id: input.student_id },
});
const text =
"\n🧑‍🤝‍🧑 ชื่อผู้ยืม: " +
student_name?.name +
"\n" +
"🤮 วันที่ยืม: " +
input.startDate?.toDateString();
const message = new FormData();
message.append("message", text);
message.append("stickerPackageId", "446");
message.append("stickerId", "2006");
await fetch("https://notify-api.line.me/api/notify", {
method: "post",
body: message,
headers: {
Authorization: "Bearer " + LINE_TOKEN,
},
});
await sendMessage(
student?.name ?? "",
input.startDate?.toDateString() ?? "",
);
});
}),

Expand Down Expand Up @@ -335,7 +331,7 @@ export const parcelRouter = createTRPCRouter({
where: {
parcel_id: input.parcel_id,
NOT: {
status: "INUSE",
status: BORROWING_STATUS.INUSE,
},
},
});
Expand Down
28 changes: 27 additions & 1 deletion src/utils/function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignJWT, jwtVerify } from "jose";
import { encryptionKey, STUDENT_ID } from "./constant";
import { encryptionKey, LINE_TOKEN, STUDENT_ID } from "./constant";
import { type Student } from "@prisma/client";

/**
Expand Down Expand Up @@ -47,3 +47,29 @@ export const decrypt = async (token: string) => {
}
}
};

/**
* Sending message to line api
* @param {string} student_name - name of the student who borrow the key;
* @param {string} start_date - the date that user want to borrow;
* @returns {Promise<void>} A promise that resolves to the decrypted data if the token is valid,
* or an error object if the token is invalid.
*/
export const sendMessage = async (
student_name: string,
start_date: string,
): Promise<void> => {
const text =
"\n🧑‍🤝‍🧑 ชื่อผู้ยืม: " + student_name + "\n" + "🤮 วันที่ยืม: " + start_date;
const message = new FormData();
message.append("message", text);
message.append("stickerPackageId", "446");
message.append("stickerId", "2006");
await fetch("https://notify-api.line.me/api/notify", {
method: "post",
body: message,
headers: {
Authorization: "Bearer " + LINE_TOKEN,
},
});
};

0 comments on commit 19eb282

Please sign in to comment.