-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathcertificateBatchController.js
More file actions
146 lines (128 loc) · 4.43 KB
/
Copy pathcertificateBatchController.js
File metadata and controls
146 lines (128 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const User = require("../models/userSchema");
const { CertificateBatch } = require("../models/certificateSchema");
const { validateBatchSchema } = require("../utils/batchValidate");
const { findEvent } = require("../services/event.service");
const { findTemplate } = require("../services/template.service");
const { getUserPosition, getApprovers } = require("../services/user.service");
const { getOrganization } = require("../services/organization.service");
const { HttpError } = require("../utils/httpError");
async function createBatch(req, res) {
//console.log(req.user);
try {
const { id, role } = req.user;
//to get user club
// positionHolders({user_id: id}) -> positions({_id: position_id}) -> organizationalUnit({_id: unit_id}) -> unit_id = "Club name"
const { name, eventId, templateId, signatoryDetails, users, action } =
req.body;
const validation = validateBatchSchema.safeParse({
name,
eventId,
templateId,
signatoryDetails,
users,
});
let lifecycleStatus, approvalStatus;
if (action === "Submitted") {
lifecycleStatus = "Submitted";
approvalStatus = "Pending";
}
if (!validation.success) {
let errors = validation.error.issues.map((issue) => issue.message);
return res.status(400).json({ message: errors });
}
const event = await findEvent(eventId);
const template = await findTemplate(templateId);
// Get coordinator's position and unit
const position = await getUserPosition(id);
const eventOrgId =
event.organizing_unit_id && event.organizing_unit_id.toString();
const positionUnitId = position.unit_id && position.unit_id.toString();
if (
eventOrgId !== positionUnitId ||
role.toUpperCase() !== position.title
) {
return res
.status(403)
.json({ message: "You are not authorized to initiate batches." });
}
// Ensure org is a Club
const club = await getOrganization(position.unit_id);
if (club.type.toLowerCase() !== "club") {
return res.status(403).json({ message: "Organization is not a Club" });
}
// Resolve General Secretary and President objects for the club
const { gensecObj, presidentObj } = await getApprovers(club.category);
const approverIds = [gensecObj._id, presidentObj._id];
// Validate user ids and existence (bulk query + duplicate detection)
const uniqueUsers = [...new Set(users.map((u) => u.toString()))];
const duplicates = uniqueUsers.length !== users.length;
if (duplicates) {
return res
.status(400)
.json({ message: "Duplicate user ids are not allowed in a batch" });
}
const existing = await User.find({ _id: { $in: users } })
.select("_id")
.lean();
const existingSet = new Set(existing.map((u) => u._id.toString()));
const missing = uniqueUsers
.filter((u) => !existingSet.has(u))
.map((uid) => ({ uid, ok: false, reason: "User not found" }));
if (missing.length > 0) {
return res
.status(400)
.json({ message: "Invalid user data sent", details: missing });
}
const newBatch = await CertificateBatch.create({
title: name,
eventId: event._id,
templateId: template._id,
initiatedBy: id,
approverIds,
approvalStatus: approvalStatus,
lifecycleStatus: lifecycleStatus || "Draft",
users: users,
signatoryDetails,
});
res.json({ message: "New Batch created successfully" });
} catch (err) {
if (err instanceof HttpError) {
const payload = { message: err.message };
if (err.details) payload.details = err.details;
return res.status(err.statusCode).json(payload);
}
res.status(500).json({ message: err.message || "Internal server error" });
}
}
async function editBatch(req, res) {
const { id } = req.user._id;
}
async function getAllBatches(req, res) {
const batches = await CertificateBatch.find().populate([
{
path: "initiatedBy",
select: "personal_info.name -_id",
},
{
path: "templateId",
select: "title ",
},
{
path: "eventId",
select: "title description organizing_unit_id schedule",
populate: {
path: "organizing_unit_id",
select: "name -_id",
},
},
]);
if (!batches) {
return res.status(404).json({ messages: "No batches found" });
}
return res.json({ message: batches });
}
module.exports = {
createBatch,
editBatch,
getAllBatches,
};