Skip to content

Commit

Permalink
chore(api): removed purifyts from createAccountProfile and getAccount…
Browse files Browse the repository at this point in the history
…FromEmail functions
  • Loading branch information
agarbe committed Jul 5, 2024
1 parent 0adbc93 commit 177efdb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 49 deletions.
53 changes: 17 additions & 36 deletions packages/reva-api/modules/account/database/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Either, Left, Maybe, Right } from "purify-ts";

import { prismaClient } from "../../../prisma/client";
import { logger } from "../../shared/logger";
import { Account } from ".prisma/client";

export const createAccountProfile = async (params: {
Expand All @@ -11,37 +8,21 @@ export const createAccountProfile = async (params: {
organismId?: string;
keycloakId: string;
certificationAuthorityId?: string;
}): Promise<Either<string, Account>> => {
try {
const account = await prismaClient.account.create({
data: {
keycloakId: params.keycloakId,
email: params.email,
firstname: params.firstname,
lastname: params.lastname,
organismId: params.organismId,
certificationAuthorityId: params.certificationAuthorityId,
},
});

return Right(account);
} catch (e) {
logger.error(e);
return Left("error while creating account");
}
};
}): Promise<Account> =>
prismaClient.account.create({
data: {
keycloakId: params.keycloakId,
email: params.email,
firstname: params.firstname,
lastname: params.lastname,
organismId: params.organismId,
certificationAuthorityId: params.certificationAuthorityId,
},
});

export const getAccountFromEmail = async (email: string) => {
try {
const account = await prismaClient.account.findUnique({
where: {
email,
},
});
return Right(Maybe.fromNullable(account));
} catch (e: unknown) {
const errorMessage = `Error while retrieving account from email - ${e}`;
logger.error(errorMessage);
return Left(errorMessage);
}
};
export const getAccountFromEmail = async (email: string) =>
prismaClient.account.findUnique({
where: {
email,
},
});
14 changes: 6 additions & 8 deletions packages/reva-api/modules/account/features/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ export const createAccount = async (params: {
const keycloakId = await IAM.createAccount(params);

//create and return the account in database
return (
await createAccountProfile({
...params,
firstname: params.firstname || "",
lastname: params.lastname || "",
keycloakId,
})
).unsafeCoerce();
return await createAccountProfile({
...params,
firstname: params.firstname || "",
lastname: params.lastname || "",
keycloakId,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ export const validateSubscriptionRequest = async ({
}

//account check
const oldAccount = (
await getAccountFromEmail(subscriptionRequest.accountEmail)
)
.unsafeCoerce()
.extractNullable();
const oldAccount = await getAccountFromEmail(
subscriptionRequest.accountEmail,
);

if (oldAccount) {
throw new Error(
Expand Down

0 comments on commit 177efdb

Please sign in to comment.