-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetUser.ts
126 lines (116 loc) · 3.5 KB
/
getUser.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
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
import { lambda, sdk } from '@pulumi/aws';
import type { IUser } from '#tables/tables/user';
import type { lambdaEvent } from '#utils/util';
import { UsersTable } from '#tables/index';
import {
deconstruct,
userEpoch,
currentEndpoint,
CUSTOM_ERROR_CODES,
makeCustomError,
populateResponse,
STATUS_CODES,
} from '#utils/util';
/**
* The getUser lambda
* @description
* - The lambda is used to get a user by their userID
* - The lambda is triggered by a GET request to /users/{userID}
*
* @see https://www.pulumi.com/docs/guides/crosswalk/aws/api-gateway/#lambda-request-handling
*/
export const getUser = new lambda.CallbackFunction<
lambdaEvent,
{
body: string;
statusCode: number;
}
>('getUser', {
runtime: lambda.Runtime.NodeJS16dX,
callback: async event => {
const { userID } = event.pathParameters!;
try {
const client = new sdk.DynamoDB.DocumentClient(currentEndpoint);
const { Items } = await client
.query({
TableName: UsersTable.get(),
IndexName: 'userID',
KeyConditionExpression: 'userID = :id',
ExpressionAttributeValues: {
':id': userID,
},
})
.promise();
if (!Items?.length) {
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('User not found', CUSTOM_ERROR_CODES.USER_NOT_FOUND),
);
}
const user = Items[0] as IUser;
delete user.password;
delete user.token;
const { timestamp } = deconstruct(user.userID!, userEpoch);
return populateResponse(STATUS_CODES.OK, { ...user, createdAt: timestamp });
} catch (error) {
if ((error as any).code === 'ConditionalCheckFailedException') {
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('User with that email already exists', CUSTOM_ERROR_CODES.USER_ALREADY_EXISTS),
);
}
console.error(error);
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('Internal Server Error', CUSTOM_ERROR_CODES.USER_ERROR),
);
}
},
});
/**
* The getUserByEmail lambda
* @description
* - The lambda is used to get a user by their email
* - The lambda is triggered by a GET request to /users/email/{email}
*
* @see https://www.pulumi.com/docs/guides/crosswalk/aws/api-gateway/#lambda-request-handling
*/
export const getUserByEmail = new lambda.CallbackFunction<
lambdaEvent,
{
body: string;
statusCode: number;
}
>('getUserByEmail', {
runtime: lambda.Runtime.NodeJS16dX,
callback: async event => {
const { email } = event.pathParameters!;
try {
const client = new sdk.DynamoDB.DocumentClient(currentEndpoint);
const { Item: user } = await client
.get({
TableName: UsersTable.get(),
Key: {
email,
},
})
.promise();
if (!user) {
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('User not found', CUSTOM_ERROR_CODES.USER_NOT_FOUND),
);
}
delete user.password;
delete user.token;
const { timestamp } = deconstruct(user.userID, userEpoch);
return populateResponse(STATUS_CODES.OK, { ...user, createdAt: timestamp });
} catch (error) {
console.error(error);
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('Internal Server Error', CUSTOM_ERROR_CODES.USER_ERROR),
);
}
},
});