Skip to content

Commit

Permalink
✨ADD : Confirmation Submit Function
Browse files Browse the repository at this point in the history
Co-authored-by: SSSBoOm <boom1909chanapat@gmail.com>
Co-authored-by: Chaiyapat Oam <ChaiyapatOam@users.noreply.github.com>
Co-authored-by: Thanaphol <Thanaphol47825@users.noreply.github.com>
  • Loading branch information
4 people committed Mar 10, 2023
1 parent 8a61a4a commit f3cda7d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
13 changes: 12 additions & 1 deletion app/controller/confirm.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ exports.updateExamination4 = async (req, res) => {
try {
const uid = req.uid;
const body = req.body;
const result = await confirmService.updateExamination1(uid, body);
const result = await confirmService.updateExamination4(uid, body);
res.status(200).send({ success: true, data: result });
} catch (error) {
console.log(error);
Expand Down Expand Up @@ -162,8 +162,19 @@ exports.getExamination5 = async (req, res) => {

exports.submit = async (req, res) => {
try {
const uid = req.uid;
const result = await confirmService.updateSubmit(uid);
if (result) {
const count = await confirmService.countSubmitted();
confirmService.sendDiscordHook(result, count);
// console.log(count);
res.status(200).send({ success: true, data: result });
} else {
res.status(405).send({ success: false, message: "User not valid Form" });
}
} catch (error) {
console.log(error);
res.status(500).send({ success: false });
}
};

95 changes: 92 additions & 3 deletions app/services/confirm.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Confirmation = prisma.confirmation;

exports.whitelistChecker = async (uid) => {
if (!uid) throw new Error("No Uid Provide");
const result =await Whitelist.findUnique({
const result = await Whitelist.findUnique({
where: {
uid: uid,
},
Expand Down Expand Up @@ -71,7 +71,7 @@ exports.updateConfirmation = async (uid, body) => {
transaction_Name: body.transaction_Name,
transaction_URL: body.transaction_URL,
transaction_date: body.transaction_date,
transaction_hours: body.updateConfirmation,
transaction_hours: body.transaction_hours,
transaction_minutes: body.transaction_minutes,
},
});
Expand All @@ -93,7 +93,7 @@ exports.getConfirmation = async (uid) => {
transaction_date: true,
transaction_hours: true,
transaction_minutes: true,
is_completed:true,
is_completed: true,
},
});
return result;
Expand Down Expand Up @@ -240,3 +240,92 @@ exports.getExamination5 = async (uid) => {
});
return result;
};

exports.updateSubmit = async (uid) => {
if (!uid) throw new Error("No Uid Provide");
const res = await Whitelist.findUnique({
where: {
uid: uid,
},
include: {
examination: true,
confirmation: true,
},
});
try {
// if not confirm
if (res.isConfirm === false) {
const _toISOString = new Date().toISOString();
await Confirmation.update({
where: {
uid: uid,
},
data: {
is_completed: true,
submited_at: _toISOString,
},
});
return res;
} else if (
res.isConfirm === true &&
res.shirt_size &&
res.describeTravel &&
res.describeBackhome &&
res.examination.q1_1 &&
res.examination.q1_2 &&
res.examination.q1_3 &&
res.examination.q2_1 &&
res.examination.q2_2 &&
res.examination.q2_3 &&
res.examination.q3_1 &&
res.examination.q3_2 &&
res.examination.q4_1 &&
res.examination.q4_2 &&
res.examination.q5_1 &&
res.examination.q5_2 &&
res.examination.q5_3
) {
const _toISOString = new Date().toISOString();
await Confirmation.update({
where: {
uid: uid,
},
data: {
is_completed: true,
submited_at: _toISOString,
},
});
return res;
} else {
return false;
}
} catch (error) {
console.log(error);
return false;
}
};

exports.sendDiscordHook = async (data,count) => {
if (!process.env.webHook) throw new Error("Forget to set env webHook");
const hook = new Webhook({ url: process.env.webHook, throwErrors: true });
const botProfile =
"https://user-images.githubusercontent.com/83873103/224265495-b5dec606-d1ad-4899-a1d2-36e17982123a.png";
hook.setUsername("Snorlax.tsx");
hook.setAvatar(botProfile);
const userName = data.nickname || "USER";
//Sends a success message
hook.success(
"**[Comcamp34 Confirmation]**",
`น้อง **${userName}** ได้ทำการยืนยันสิทธิ์มาแล้ว ✨`,
`เป็นลำดับที่ **${count}**`
);
};

exports.countSubmitted = async () => {
const count = await Confirmation.count({
where: {
is_completed: true,
},
});
return count;
};
8 changes: 4 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ model examination {
q2_1 String? @db.VarChar(4)
q2_2 String? @db.VarChar(4)
q2_3 String? @db.VarChar(4)
q3_1 String? @db.VarChar(255)
q3_2 String? @db.VarChar(255)
q4_1 String? @db.VarChar(255)
q4_2 String? @db.VarChar(255)
q3_1 String? @db.VarChar(255) @default("[]")
q3_2 String? @db.VarChar(255) @default("[]")
q4_1 String? @db.VarChar(255) @default("[]")
q4_2 String? @db.VarChar(255) @default("[]")
q5_1 String? @db.VarChar(4)
q5_2 String? @db.VarChar(4)
q5_3 String? @db.VarChar(4)
Expand Down

0 comments on commit f3cda7d

Please sign in to comment.