targo-backend/src/identity-and-account/users-management/services/abstract-user.service.ts

43 lines
1.1 KiB
TypeScript

import { Injectable, NotFoundException } from '@nestjs/common';
import { Modules, Users } from 'prisma/postgres/generated/prisma/client/postgres/client';
import { toKeysFromBoolean } from 'src/common/utils/boolean-utils';
import { PrismaPostgresService } from 'prisma/postgres/prisma-postgres.service';
@Injectable()
export abstract class AbstractUserService {
constructor(protected readonly prisma: PrismaPostgresService) { }
async findOneByEmail(
email: string
): Promise<Partial<Users>> {
const user = await this.prisma.users.findUnique({
where: { email },
include: {
user_module_access: {
omit: {
id: true,
user_id: true,
},
},
},
});
if (!user) {
throw new NotFoundException(`No user with email #${email} exists`);
}
let module_access: Modules[] = [];
if (user.user_module_access !== null) module_access = toKeysFromBoolean(user.user_module_access);
const clean_user = {
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
role: user.role,
user_module_access: module_access,
}
return clean_user;
}
}