-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateUser.ts
95 lines (86 loc) · 2.52 KB
/
updateUser.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
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
import { lambda, sdk } from '@pulumi/aws';
import { getToken } from '../../auth';
import type { IUser } from '#tables/tables/user';
import type { lambdaEvent } from '#utils/util';
import { UsersTable } from '#tables/index';
import { validateUserBody } from '#tables/validation/users';
import {
currentEndpoint,
CUSTOM_ERROR_CODES,
makeCustomError,
updateObject,
decodeJWT,
populateResponse,
STATUS_CODES,
} from '#utils/util';
/**
* The updateUser lambda
* @description
* - The lambda is used to update a user
* - The lambda is triggered by a PATCH request to /users/update/{userID}
*
* @see https://www.pulumi.com/docs/guides/crosswalk/aws/api-gateway/#lambda-request-handling
*/
export const updateUser = new lambda.CallbackFunction<
lambdaEvent,
{
body: string;
statusCode: number;
}
>('updateUser', {
runtime: lambda.Runtime.NodeJS16dX,
callback: async event => {
const email = decodeJWT(getToken(event)).data?.email;
if (!email) {
return populateResponse(
STATUS_CODES.UNAUTHORIZED,
makeCustomError('Unauthorized', CUSTOM_ERROR_CODES.USER_ERROR),
);
}
const { parsed, error } = validateUserBody(event, {});
if (!parsed || error) {
return populateResponse(
STATUS_CODES.BAD_REQUEST,
makeCustomError(error ?? 'Bad Request', CUSTOM_ERROR_CODES.BODY_NOT_VALID),
);
}
const client = new sdk.DynamoDB.DocumentClient(currentEndpoint);
const { name, tags } = parsed as IUser;
const updateObj = {
...(name && { name }),
...(tags && { tags }),
...(name || tags ? { updated: Date.now() } : {}),
};
if (!Object.keys(updateObj).length) {
return populateResponse(
STATUS_CODES.BAD_REQUEST,
makeCustomError('No fields to update', CUSTOM_ERROR_CODES.USER_ERROR),
);
}
const { ExpressionAttributeNames, ExpressionAttributeValues, UpdateExpression } = updateObject(updateObj);
try {
await client
.update({
TableName: UsersTable.get(),
Key: {
email,
},
UpdateExpression,
ExpressionAttributeNames,
ExpressionAttributeValues,
})
.promise();
return populateResponse(STATUS_CODES.OK, {
patchedFields: {
...updateObj,
},
});
} catch (error) {
console.error(error);
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('Internal Server Error', CUSTOM_ERROR_CODES.USER_ERROR),
);
}
},
});