/
addFeedback.ts
66 lines (57 loc) · 1.78 KB
/
addFeedback.ts
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
import {
EVENT_NOT_FOUND_ERROR,
USER_NOT_CHECKED_IN,
USER_NOT_REGISTERED_FOR_EVENT,
FEEDBACK_ALREADY_SUBMITTED,
} from "../../constants";
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { errors, requestContext } from "../../libraries";
import { Event, EventAttendee, CheckIn, Feedback } from "../../models";
export const addFeedback: MutationResolvers["addFeedback"] = async (
_parent,
args,
context,
) => {
const currentEventExists = await Event.exists({
_id: args.data.eventId,
});
if (!currentEventExists) {
throw new errors.NotFoundError(
requestContext.translate(EVENT_NOT_FOUND_ERROR.MESSAGE),
EVENT_NOT_FOUND_ERROR.CODE,
EVENT_NOT_FOUND_ERROR.PARAM,
);
}
const eventAttendeeObject = await EventAttendee.findOne({
eventId: args.data.eventId,
userId: context.userId,
})
.populate("checkInId")
.lean();
if (eventAttendeeObject === null) {
throw new errors.ConflictError(
requestContext.translate(USER_NOT_REGISTERED_FOR_EVENT.MESSAGE),
USER_NOT_REGISTERED_FOR_EVENT.CODE,
USER_NOT_REGISTERED_FOR_EVENT.PARAM,
);
}
if (eventAttendeeObject.checkInId === null) {
throw new errors.ConflictError(
requestContext.translate(USER_NOT_CHECKED_IN.MESSAGE),
USER_NOT_CHECKED_IN.CODE,
USER_NOT_CHECKED_IN.PARAM,
);
}
if (eventAttendeeObject.checkInId.feedbackSubmitted) {
throw new errors.ConflictError(
requestContext.translate(FEEDBACK_ALREADY_SUBMITTED.MESSAGE),
FEEDBACK_ALREADY_SUBMITTED.CODE,
FEEDBACK_ALREADY_SUBMITTED.PARAM,
);
}
await CheckIn.findByIdAndUpdate(eventAttendeeObject.checkInId, {
feedbackSubmitted: true,
});
const feedback = await Feedback.create({ ...args.data });
return feedback;
};