teraskill-be / src /modules /admin /admin.service.ts
Farhan Dwi Hartanto
Add application file
545f89a
Raw
History Blame Contribute Delete
1.49 kB
import { ConflictException, Injectable } from '@nestjs/common';
import bcrypt from 'bcrypt';
import { v4 as uuidv4 } from 'uuid';
import { PrismaService } from '../database/prisma.service';
import { CreateAdminDto } from './dto/create-admin.dto';
@Injectable()
export class AdminService {
constructor(private readonly prisma: PrismaService) {}
async create(dto: CreateAdminDto) {
// Is the email already registered
const existingAdmin = await this.prisma.users.findUnique({
where: { email: dto.email },
});
if (existingAdmin) {
throw new ConflictException('Admin with this email already exists');
}
const hashedPassword = await bcrypt.hash(dto.password, 10);
// Create new admin
const newAdmin = await this.prisma.users.create({
data: {
id: uuidv4(),
name: dto.name,
email: dto.email,
phoneNumber: dto.phoneNumber,
role: dto.role,
image: `https://ui-avatars.com/api/?background=1565D8&color=fff&rounded=true&size=128&length=2&bold=true&font-size=0.33&name=${dto.name}`,
emailVerified: true,
accounts: {
create: [
{
id: uuidv4(),
accountId: uuidv4(),
providerId: 'credential',
password: hashedPassword,
},
],
},
},
select: {
id: true,
email: true,
role: true,
name: true,
},
});
return newAdmin;
}
}