🚧 wip: rewrite reprovision logic to match new user schema

This commit is contained in:
2026-05-28 21:01:54 +07:00
parent 8cebc0cd20
commit 57d19d4302
13 changed files with 120 additions and 110 deletions

View File

@ -1,39 +1,29 @@
import { User } from "@prisma/client";
import { UserHeaderInformation } from "../../../../helpers/http/userHeader/getUserHeaderInformation/types";
import { findUserService } from "../../../user/services/internal/findUser.service";
import { createUserSessionService } from "../../../userSession/services/createUserSession.service";
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
import { createUserViaOauth } from "../../../user/user.types";
import { createUserService } from "../../../user/services/internal/createUser.service";
import { AppError } from "../../../../helpers/error/instances/app";
import { findAuthIdentityByEmailAndProviderRepository } from "../../repositories/READ/findAuthIdentityByEmailAndProvider.repository";
export const OAuthUserProvisionService = async (
payload: createUserViaOauth,
userHeaderInfo: UserHeaderInformation
) => {
export const OAuthUserProvisionService = async (payload: createUserViaOauth, userHeaderInfo: UserHeaderInformation) => {
try {
const providerId = payload.providerId;
const findUserResult = (await findUserService({
identifier: providerId,
queryTarget: "providerId",
options: { verbosity: "full" },
})) as User;
if (findUserResult) {
return await createUserSessionService(findUserResult.id, userHeaderInfo);
} else {
const findUserByEmailOnly = await findUserService({
identifier: payload.email,
queryTarget: "email",
options: { verbosity: "exist" },
});
if (findUserByEmailOnly)
throw new AppError(409, "Email already in use with another account");
const checkExistingUser = await findAuthIdentityByEmailAndProviderRepository(payload.email);
if (
checkExistingUser &&
checkExistingUser.oauth_accounts.some((account) => account.provider_sub === payload.oauthProvider.sub)
) {
// User already exists with this OAuth provider
return await createUserSessionService(checkExistingUser.id, userHeaderInfo);
} else if (!checkExistingUser) {
// No user with this email, create new user
const createdUser = await createUserService(payload);
return await createUserSessionService(createdUser.id, userHeaderInfo);
}
// User exists with this email but not with this OAuth provider
throw new AppError(409, "Email already in use with another account");
} catch (error) {
ErrorForwarder(error);
}