authService

 avatar
unknown
typescript
9 months ago
1.7 kB
14
Indexable
export const checkUsernameAvailability = async (username: string) => {
    return prisma.user.findUnique({
        where: {
            username: username
        },
        select: {
            id: true
        }
    })
};

// ---------------------------------------------------------------------------------------------------------

export const checkEmailAvailability = async (email: string) => {
    const user = prisma.user.findUnique({
        where: {
            email: email
        },
        select: {
            id: true
        }
    });

    if (user) return user;

    const tempUser = prisma.temporaryUser.findUnique({
        where: {
            email: email
        },
        select: {
            id: true
        }
    });

    return tempUser;
};

// ---------------------------------------------------------------------------------------------------------

export const createTemporaryUser = async (profileName: string, email: string, dateOfBirth: Date, hashedPassword: string) => {
    try {
        return await prisma.temporaryUser.create({
            data: {
                profileName,
                email,
                dateOfBirth,
                password: hashedPassword
            },
        });
    } catch (error) {
        if (error instanceof Prisma.PrismaClientKnownRequestError) {
            if (error.code === 'P2002') {
                // Unique constraint violation (e.g. username or email already exists)
                return { error: 'Unique constraint violation', fields: (error.meta?.target as string[]) ?? [] };
            }
        }

        return { error: getErrorMessage(error), fields: [] };
    }
};
Editor is loading...
Leave a Comment