Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import { AuthenticationError } from 'apollo-server-express';
2
3import { isCoopErrorOfType } from '../../utils/errors.js';
4import {
5 type GQLMutationResolvers,
6 type GQLQueryResolvers,
7} from '../generated.js';
8
9const typeDefs = /* GraphQL */ `
10 enum TextBankType {
11 STRING
12 REGEX
13 }
14
15 type TextBank {
16 id: ID!
17 name: String!
18 description: String
19 type: TextBankType!
20 strings: [String!]!
21 }
22
23 input CreateTextBankInput {
24 name: String!
25 description: String
26 type: TextBankType!
27 strings: [String!]!
28 }
29
30 input UpdateTextBankInput {
31 id: ID!
32 name: String
33 description: String
34 type: TextBankType
35 strings: [String!]
36 }
37
38 type MutateBankResponse {
39 success: Boolean
40 error: String
41 }
42
43 type Query {
44 textBank(id: ID!): TextBank
45 }
46
47 type Mutation {
48 createTextBank(input: CreateTextBankInput!): MutateBankResponse!
49 updateTextBank(input: UpdateTextBankInput!): MutateBankResponse!
50 deleteTextBank(id: ID!): Boolean!
51 }
52`;
53
54const Query: GQLQueryResolvers = {
55 async textBank(_, { id }, context) {
56 const user = context.getUser();
57 if (user == null) {
58 throw new AuthenticationError('Authenticated user required');
59 }
60
61 try {
62 const textBank =
63 await context.services.ModerationConfigService.getTextBank({
64 id,
65 orgId: user.orgId,
66 });
67
68 return textBank;
69 } catch (e) {
70 if (isCoopErrorOfType(e, 'NotFoundError')) {
71 return null;
72 }
73 throw e;
74 }
75 },
76};
77
78const Mutation: GQLMutationResolvers = {
79 async createTextBank(_, params, context) {
80 const user = context.getUser();
81 if (user == null) {
82 throw new AuthenticationError('User required.');
83 }
84
85 const { name, description, type, strings } = params.input;
86
87 try {
88 await context.services.ModerationConfigService.createTextBank(
89 user.orgId,
90 {
91 name,
92 description: description ?? null,
93 type,
94 ownerId: null,
95 strings: [...strings],
96 },
97 );
98 return { success: true };
99 } catch (e) {
100 if (isCoopErrorOfType(e, 'MatchingBankNameExistsError')) {
101 return { success: false, error: e.message };
102 }
103
104 throw e;
105 }
106 },
107 async updateTextBank(_, params, context) {
108 const user = context.getUser();
109 if (user == null) {
110 throw new AuthenticationError('User required.');
111 }
112
113 const { id, name, description, type, strings } = params.input;
114
115 try {
116 await context.services.ModerationConfigService.updateTextBank(
117 user.orgId,
118 {
119 id,
120 name: name ?? undefined,
121 description: description ?? null,
122 type: type ?? undefined,
123 strings: strings ? [...strings] : undefined,
124 },
125 );
126 return { success: true };
127 } catch (e) {
128 if (isCoopErrorOfType(e, 'MatchingBankNameExistsError')) {
129 return { success: false, error: e.message };
130 }
131
132 throw e;
133 }
134 },
135 async deleteTextBank(_, params, context) {
136 const user = context.getUser();
137 if (user == null) {
138 throw new AuthenticationError('Authenticated user required');
139 }
140
141 try {
142 const result =
143 await context.services.ModerationConfigService.deleteTextBank(
144 user.orgId,
145 params.id,
146 );
147 return result;
148 } catch (error) {
149 return false;
150 }
151 },
152};
153
154const resolvers = {
155 Query,
156 Mutation,
157};
158
159export { typeDefs, resolvers };