All files / src/features/staff staff.service.ts

88.23% Statements 15/17
75% Branches 9/12
100% Functions 6/6
87.5% Lines 14/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                      16x             1x                         2x       2x 1x                   2x 2x               1x 1x                           2x 2x         1x 1x                       1x          
import { Injectable, ConflictException, NotFoundException } from '@nestjs/common'
import { PrismaService } from '../../database/prisma.service'
import { CreateStaffDto, UpdateStaffDto } from '@fundraising/types'
import { Prisma } from '@prisma/client'
 
@Injectable()
export class StaffService {
    /**
     * Initialize StaffService.
     * @param prisma - PrismaService instance
     */
    constructor(private prisma: PrismaService) {}
 
    /**
     * Retrieve all staff members with event counts.
     * @returns List of staff members.
     */
    async findAll() {
        return this.prisma.staffMember.findMany({
            orderBy: { createdAt: 'desc' },
            include: { _count: { select: { events: true } } },
        })
    }
 
    /**
     * Retrieve a single staff member by ID.
     * @param id - Staff ID
     * @returns Staff member details.
     * @throws NotFoundException if not found.
     */
    async findOne(id: string) {
        const staff = await this.prisma.staffMember.findUnique({
            where: { id },
            include: { events: true },
        })
        if (!staff) throw new NotFoundException('Staff member not found')
        return staff
    }
 
    /**
     * Create a new staff member.
     * @param data - Creation data (DTO).
     * @returns Created staff member.
     * @throws ConflictException if PIN is already in use.
     */
    async create(data: CreateStaffDto) {
        try {
            return await this.prisma.staffMember.create({
                data: {
                    name: data.name,
                    code: data.code,
                    ...(data.eventId ? { events: { connect: { id: data.eventId } } } : {}),
                },
            })
        } catch (error) {
            Eif (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
                throw new ConflictException('PIN code already in use')
            }
            throw error
        }
    }
 
    /**
     * Update an existing staff member.
     * @param id - Staff ID.
     * @param data - Update data (DTO).
     * @returns Updated staff member.
     * @throws ConflictException if PIN is already in use.
     */
    async update(id: string, data: UpdateStaffDto) {
        try {
            return await this.prisma.staffMember.update({
                where: { id },
                data,
            })
        } catch (error) {
            Eif (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
                throw new ConflictException('PIN code already in use')
            }
            throw error
        }
    }
 
    /**
     * Remove a staff member.
     * @param id - Staff ID.
     * @returns Deleted staff member.
     */
    async remove(id: string) {
        return this.prisma.staffMember.delete({
            where: { id },
        })
    }
}