Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/update api key #212

Merged
merged 11 commits into from
Aug 24, 2023
38 changes: 16 additions & 22 deletions services/core/functions/update-user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,46 @@ import { APIGatewayProxyEvent } from "aws-lambda";

import { UserEntity } from "../../entities";
import { encryptKey } from "./encryptKey";
import { formatResponse } from "../../helpers/format-response";

interface UpdateUserLambdaInput {
apiKey: string;
userId: string;
email: string;
Comment on lines -8 to +9
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary and will be changed in the next ticket

}

export const main = async (event: APIGatewayProxyEvent) => {
if (event.body == null) {
return {
statusCode: 400,
body: "The request does not contain a body as expected.",
};
return formatResponse(
"The request does not contain a body as expected.",
400
);
}

try {
const inputBody = JSON.parse(event.body) as UpdateUserLambdaInput;
const apiKey = inputBody.apiKey;
const userId = inputBody.userId;
const email = inputBody.email;

if (apiKey === undefined || userId === undefined) {
return {
statusCode: 400,
body: "The request body does not contain the expected data.",
};
if (apiKey === undefined || email === undefined) {
return formatResponse(
"The request body does not contain the expected data.",
400
);
}

const encryptedApiKey = await encryptKey(apiKey);

await UserEntity.update(
{
userId: userId,
email: email,
apiKey: encryptedApiKey,
},
{ conditions: { attr: "userId", exists: true } }
{ conditions: { attr: "email", exists: true } }
);

return {
statusCode: 200,
body: "User updated successfully.",
};
return formatResponse("User updated successfully.");
} catch (err) {
console.error(err);

return {
statusCode: 500,
body: "Error when updating user.",
};
return formatResponse("Error when updating user.", 500);
}
};
Loading