forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-organization.js
More file actions
238 lines (224 loc) · 6.83 KB
/
create-organization.js
File metadata and controls
238 lines (224 loc) · 6.83 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const { GraphQLNonNull, GraphQLString } = require('graphql')
const { mutationWithClientMutationId } = require('graphql-relay')
const { t } = require('@lingui/macro')
const { Acronym } = require('../../scalars')
const { organizationType } = require('../../types')
const createOrganization = new mutationWithClientMutationId({
name: 'CreateOrganization',
description:
'This mutation allows the creation of an organization inside the database.',
inputFields: () => ({
acronymEN: {
type: GraphQLNonNull(Acronym),
description: 'The English acronym of the organization.',
},
acronymFR: {
type: GraphQLNonNull(Acronym),
description: 'The French acronym of the organization.',
},
nameEN: {
type: GraphQLNonNull(GraphQLString),
description: 'The English name of the organization.',
},
nameFR: {
type: GraphQLNonNull(GraphQLString),
description: 'The French name of the organization.',
},
zoneEN: {
type: GraphQLNonNull(GraphQLString),
description:
'The English translation of the zone the organization belongs to.',
},
zoneFR: {
type: GraphQLNonNull(GraphQLString),
description:
'The English translation of the zone the organization belongs to.',
},
sectorEN: {
type: GraphQLNonNull(GraphQLString),
description:
'The English translation of the sector the organization belongs to.',
},
sectorFR: {
type: GraphQLNonNull(GraphQLString),
description:
'The French translation of the sector the organization belongs to.',
},
countryEN: {
type: GraphQLNonNull(GraphQLString),
description:
'The English translation of the country the organization resides in.',
},
countryFR: {
type: GraphQLNonNull(GraphQLString),
description:
'The French translation of the country the organization resides in.',
},
provinceEN: {
type: GraphQLNonNull(GraphQLString),
description:
'The English translation of the province the organization resides in.',
},
provinceFR: {
type: GraphQLNonNull(GraphQLString),
description:
'The French translation of the province the organization resides in.',
},
cityEN: {
type: GraphQLNonNull(GraphQLString),
description:
'The English translation of the city the organization resides in.',
},
cityFR: {
type: GraphQLNonNull(GraphQLString),
description:
'The French translation of the city the organization resides in.',
},
}),
outputFields: () => ({
organization: {
type: organizationType,
description: 'The newly created organization.',
resolve: async (payload) => {
return payload.organization
},
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
request,
collections,
transaction,
query,
userId,
auth: { userRequired },
loaders: { orgLoaderBySlug, userLoaderByKey },
validators: { cleanseInput, slugify },
},
) => {
// Cleanse Input
const acronymEN = cleanseInput(args.acronymEN)
const acronymFR = cleanseInput(args.acronymFR)
const nameEN = cleanseInput(args.nameEN)
const nameFR = cleanseInput(args.nameFR)
const zoneEN = cleanseInput(args.zoneEN)
const zoneFR = cleanseInput(args.zoneFR)
const sectorEN = cleanseInput(args.sectorEN)
const sectorFR = cleanseInput(args.sectorFR)
const countryEN = cleanseInput(args.countryEN)
const countryFR = cleanseInput(args.countryFR)
const provinceEN = cleanseInput(args.provinceEN)
const provinceFR = cleanseInput(args.provinceFR)
const cityEN = cleanseInput(args.cityEN)
const cityFR = cleanseInput(args.cityFR)
// Create EN and FR slugs
const slugEN = slugify(nameEN)
const slugFR = slugify(nameFR)
// Get user
const user = await userRequired(userId, userLoaderByKey)
// Check to see if org already exists
const [orgEN, orgFR] = await orgLoaderBySlug.loadMany([slugEN, slugFR])
if (typeof orgEN !== 'undefined' || typeof orgFR !== 'undefined') {
console.warn(
`User: ${userId} attempted to create an organization that already exists: ${slugEN}`,
)
throw new Error(
i18n._(t`Unable to create organization. Please try again.`),
)
}
// Create new organization
const organizationDetails = {
blueCheck: false,
orgDetails: {
en: {
slug: slugEN,
acronym: acronymEN,
name: nameEN,
zone: zoneEN,
sector: sectorEN,
country: countryEN,
province: provinceEN,
city: cityEN,
},
fr: {
slug: slugFR,
acronym: acronymFR,
name: nameFR,
zone: zoneFR,
sector: sectorFR,
country: countryFR,
province: provinceFR,
city: cityFR,
},
},
}
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Trans action
const trx = await transaction(collectionStrings)
let cursor
try {
cursor = await trx.run(
async () =>
await query`
INSERT ${organizationDetails} INTO organizations
RETURN MERGE({ _id: NEW._id, _key: NEW._key, _rev: NEW._rev, blueCheck: NEW.blueCheck }, TRANSLATE(${request.language}, NEW.orgDetails))
`,
)
} catch (err) {
console.error(
`Transaction error occurred when user: ${userId} was creating new organization ${slugEN}: ${err}`,
)
throw new Error(
i18n._(t`Unable to create organization. Please try again.`),
)
}
const organization = await cursor.next()
try {
await trx.run(
async () =>
await query`
INSERT {
_from: ${organization._id},
_to: ${user._id},
permission: "admin"
} INTO affiliations
`,
)
} catch (err) {
console.error(
`Transaction error occurred when inserting edge definition for user: ${userId} to ${slugEN}: ${err}`,
)
throw new Error(
i18n._(t`Unable to create organization. Please try again.`),
)
}
try {
await trx.commit()
} catch (err) {
console.error(
`Transaction error occurred when committing new organization: ${slugEN} for user: ${userId} to db: ${err}`,
)
throw new Error(
i18n._(t`Unable to create organization. Please try again.`),
)
}
console.info(
`User: ${userId} successfully created a new organization: ${slugEN}`,
)
return {
organization: {
id: organization._key,
...organization,
},
}
},
})
module.exports = {
createOrganization,
}