-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
292 lines (255 loc) · 6.48 KB
/
index.js
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* Amplify Params - DO NOT EDIT
API_WEB_GRAPHQLAPIENDPOINTOUTPUT
API_WEB_GRAPHQLAPIIDOUTPUT
ENV
REGION
Amplify Params - DO NOT EDIT */
import crypto from '@aws-crypto/sha256-js';
import { defaultProvider } from '@aws-sdk/credential-provider-node';
import { SignatureV4 } from '@aws-sdk/signature-v4';
import { HttpRequest } from '@aws-sdk/protocol-http';
import { default as fetch, Request } from 'node-fetch';
import {
SQSClient,
DeleteMessageCommand
} from "@aws-sdk/client-sqs";
const GRAPHQL_ENDPOINT = process.env.API_WEB_GRAPHQLAPIENDPOINTOUTPUT;
const AWS_REGION = process.env.AWS_REGION;
const ENV = process.env.ENV;
const { Sha256 } = crypto;
const sqsClient = new SQSClient({ region: AWS_REGION })
const createLawyerQueueUrl = `https://sqs.us-west-1.amazonaws.com/213993515054/CreateLawyer-${ENV}`
export const signupsByEmailQuery = /* GraphQL */ `
query SignupsByEmail(
$email: AWSEmail!
$sortDirection: ModelSortDirection
$filter: ModelSignupFilterInput
$limit: Int
$nextToken: String
) {
signupsByEmail(
email: $email
sortDirection: $sortDirection
filter: $filter
limit: $limit
nextToken: $nextToken
) {
items {
id
email
firstName
lastName
cognitoID
userID
createdAt
updatedAt
_version
_deleted
_lastChangedAt
__typename
}
nextToken
startedAt
__typename
}
}
`;
export const createLawyerMutation = /* GraphQL */ `
mutation CreateLawyer(
$input: CreateLawyerInput!
$condition: ModelLawyerConditionInput
) {
createLawyer(input: $input, condition: $condition) {
id
userID
firstName
lastName
barAssociation
licenseNumber
email
phone
owner
createdAt
updatedAt
_version
_deleted
_lastChangedAt
__typename
}
}
`;
export const createPracticeMutation = /* GraphQL */ `
mutation CreatePractice(
$input: CreatePracticeInput!
$condition: ModelPracticeConditionInput
) {
createPractice(input: $input, condition: $condition) {
id
name
owner
createdAt
updatedAt
_version
_deleted
_lastChangedAt
__typename
}
}
`;
export const createPracticeLawyerMutation = /* GraphQL */ `
mutation CreatePracticeLawyer(
$input: CreatePracticeLawyerInput!
$condition: ModelPracticeLawyerConditionInput
) {
createPracticeLawyer(input: $input, condition: $condition) {
id
practiceId
lawyerId
createdAt
updatedAt
_version
_deleted
_lastChangedAt
owner
__typename
}
}
`;
export const deleteSignupMutation = /* GraphQL */ `
mutation DeleteSignup(
$input: DeleteSignupInput!
$condition: ModelSignupConditionInput
) {
deleteSignup(input: $input, condition: $condition) {
id
email
firstName
lastName
cognitoID
userID
createdAt
updatedAt
_version
_deleted
_lastChangedAt
__typename
}
}
`;
const createRequest = (endpoint, query, variables) => new HttpRequest({
method: 'POST',
headers: {
'Content-Type': 'application/json',
host: endpoint.host
},
hostname: endpoint.host,
body: JSON.stringify({ query: query, variables: variables }),
path: endpoint.pathname
});
const graphqlOperation = async (query, variables) => {
const endpoint = new URL(GRAPHQL_ENDPOINT);
const signer = new SignatureV4({
credentials: defaultProvider(),
region: AWS_REGION,
service: 'appsync',
sha256: Sha256
});
const requestToBeSigned = createRequest(endpoint, query, variables)
const signed = await signer.sign(requestToBeSigned);
const request = new Request(endpoint, signed);
let statusCode = 200;
let body;
let response;
try {
response = await fetch(request);
body = await response.json();
if (body.errors) {
statusCode = 400;
console.error(`${statusCode} ERROR: ${JSON.stringify(body.errors)}`);
}
} catch (error) {
statusCode = 500;
console.error(`${statusCode} ERROR: ${error.message}`);
}
return body;
}
/**
* @type {import('@types/aws-lambda').APIGatewayProxyHandler}
*/
export const handler = async (event) => {
const { body, receiptHandle } = event.Records[0]
const bodyObj = JSON.parse(body)
const { email } = bodyObj;
// get Signup
let signup;
try {
const signupsByEmailVariables = { email }
const signups = await graphqlOperation(signupsByEmailQuery, signupsByEmailVariables)
signup = signups.data.signupsByEmail.items[0];
} catch (error) {
console.error("getting signup:", error.message);
}
// create Practice
let practice;
try {
const practiceVariables = {
input: {
owner: signup.cognitoID,
}
};
const response = await graphqlOperation(createPracticeMutation, practiceVariables)
practice = response.data.createPractice;
} catch (error) {
console.error("creating practice:", error.message);
}
// create Lawyer
let lawyer;
try {
const { firstName, lastName, email, userID, cognitoID } = signup;
const createLawyerVariables = {
input: {
firstName,
lastName,
email,
userID,
owner: cognitoID,
}
};
const response = await graphqlOperation(createLawyerMutation, createLawyerVariables)
lawyer = response.data.createLawyer;
} catch (error) {
console.error("creating signup:", error.message);
}
// create PracticeLawyer
try {
const practiceLawyerVariables = {
input: {
practiceId: practice.id,
lawyerId: lawyer.id
}
}
await graphqlOperation(createPracticeLawyerMutation, practiceLawyerVariables);
} catch (error) {
console.error("creating practice-lawyer:", error.message);
}
// delete Signup
try {
const { id, _version } = signup
const deleteSignupVariables = { input: { id, _version } }
await graphqlOperation(deleteSignupMutation, deleteSignupVariables)
} catch (error) {
console.error("deleting signup:", error.message);
}
// delete message from CreateLawyer queue
try {
const deleteMessageInput = {
QueueUrl: createLawyerQueueUrl,
ReceiptHandle: receiptHandle
}
const deleteMessageCommand = new DeleteMessageCommand(deleteMessageInput)
await sqsClient.send(deleteMessageCommand)
} catch (error) {
console.error("deleting message from CreateLawyer queue.");
}
return { statusCode: 201} ;
};