backend/src/graphql/trip-alert/index.ts (view raw)
1import { errors } from "@strapi/utils";
2
3export default [
4 ({ nexus, strapi }) => ({
5 types: [
6 nexus.queryField("eventTripAlert", {
7 type: "TripAlertEntityResponse",
8 args: {
9 event: nexus.nonNull("ID"),
10 },
11 }),
12 nexus.mutationField("setTripAlert", {
13 type: "TripAlertEntityResponse",
14 args: {
15 event: nexus.nonNull("ID"),
16 enabled: "Boolean",
17 latitude: "Float",
18 longitude: "Float",
19 radius: "Float",
20 address: "String",
21 },
22 }),
23 ],
24 resolvers: {
25 Query: {
26 eventTripAlert: {
27 async resolve(_root, args, context) {
28 const user = context.state.user;
29 if (!user) throw new errors.ForbiddenError("No user found");
30
31 const [existingAlert] = await strapi.entityService.findMany(
32 "api::trip-alert.trip-alert",
33 {
34 filters: {
35 user: user.id,
36 event: args.event,
37 },
38 }
39 );
40
41 const { toEntityResponse } = strapi
42 .plugin("graphql")
43 .service("format").returnTypes;
44 return toEntityResponse(existingAlert, {
45 args,
46 resourceUID: "api::trip-alert.trip-alert",
47 });
48 },
49 },
50 },
51 Mutation: {
52 setTripAlert: {
53 async resolve(_root, args, context) {
54 const user = context.state.user;
55 if (!user) throw new errors.ForbiddenError("No user found");
56
57 const [existingAlert] = await strapi.entityService.findMany(
58 "api::trip-alert.trip-alert",
59 {
60 filters: {
61 user: user.id,
62 event: args.event,
63 },
64 populate: ["event"],
65 }
66 );
67
68 let tripAlert;
69 if (existingAlert)
70 tripAlert = await strapi.entityService.update(
71 "api::trip-alert.trip-alert",
72 existingAlert.id,
73 {
74 data: {
75 ...args,
76 event: existingAlert.event?.id,
77 user: user.id,
78 },
79 }
80 );
81 else
82 tripAlert = await strapi.entityService.create(
83 "api::trip-alert.trip-alert",
84 {
85 data: {
86 ...args,
87 user: user.id,
88 },
89 }
90 );
91
92 const { toEntityResponse } = strapi
93 .plugin("graphql")
94 .service("format").returnTypes;
95 return toEntityResponse(tripAlert, {
96 args,
97 resourceUID: "api::trip-alert.trip-alert",
98 });
99 },
100 },
101 },
102 },
103 resolversConfig: {
104 "Query.eventTripAlert": {
105 auth: true,
106 },
107 "Mutation.setTripAlert": {
108 auth: true,
109 },
110 },
111 }),
112];