init equipment & change-status
This commit is contained in:
119
server/src/modules/equipment/equipment.service.ts
Normal file
119
server/src/modules/equipment/equipment.service.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { EquipmentStatus, Prisma } from '@prisma/client';
|
||||
import { Response } from 'express';
|
||||
import { setListHeaders } from '../../common/http';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { CreateEquipmentDto } from './dto/create-equipment.dto';
|
||||
import { UpdateEquipmentDto } from './dto/update-equipment.dto';
|
||||
|
||||
@Injectable()
|
||||
export class EquipmentService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(query: Record<string, unknown>, response: Response) {
|
||||
const start = Number(query._start ?? 0);
|
||||
const end = Number(query._end ?? start + 25);
|
||||
const take = Math.max(end - start, 0);
|
||||
const sortField = typeof query._sort === 'string' ? query._sort : 'id';
|
||||
const sortOrder = query._order === 'DESC' ? 'desc' : 'asc';
|
||||
const q = typeof query.q === 'string' ? query.q.trim() : '';
|
||||
const rawStatus = query.status;
|
||||
const statuses = Array.isArray(rawStatus)
|
||||
? rawStatus.filter((value): value is EquipmentStatus => typeof value === 'string')
|
||||
: typeof rawStatus === 'string'
|
||||
? [rawStatus as EquipmentStatus]
|
||||
: [];
|
||||
|
||||
const where: Prisma.EquipmentWhereInput = {};
|
||||
|
||||
if (q) {
|
||||
where.OR = [
|
||||
{ name: { contains: q, mode: 'insensitive' } },
|
||||
{ serialNumber: { contains: q, mode: 'insensitive' } },
|
||||
];
|
||||
}
|
||||
|
||||
if (statuses.length === 1) {
|
||||
where.status = statuses[0];
|
||||
} else if (statuses.length > 1) {
|
||||
where.status = { in: statuses };
|
||||
}
|
||||
|
||||
const orderByField = sortField === 'id' ? 'id' : sortField;
|
||||
const [total, items] = await this.prisma.$transaction([
|
||||
this.prisma.equipment.count({ where }),
|
||||
this.prisma.equipment.findMany({
|
||||
where,
|
||||
skip: start,
|
||||
take,
|
||||
orderBy: { [orderByField]: sortOrder },
|
||||
}),
|
||||
]);
|
||||
|
||||
setListHeaders(response, total, start, end);
|
||||
return items.map((item) => this.toRecord(item));
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const item = await this.prisma.equipment.findUnique({ where: { id } });
|
||||
if (!item) {
|
||||
throw new NotFoundException(`Equipment ${id} not found`);
|
||||
}
|
||||
|
||||
return this.toRecord(item);
|
||||
}
|
||||
|
||||
async create(dto: CreateEquipmentDto) {
|
||||
const created = await this.prisma.equipment.create({
|
||||
data: {
|
||||
name: dto.name,
|
||||
serialNumber: dto.serialNumber,
|
||||
dateOfInspection: dto.dateOfInspection ? new Date(dto.dateOfInspection) : undefined,
|
||||
commissionedAt: dto.commissionedAt ? new Date(dto.commissionedAt) : undefined,
|
||||
status: dto.status,
|
||||
},
|
||||
});
|
||||
|
||||
return this.toRecord(created);
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateEquipmentDto) {
|
||||
await this.findOne(id);
|
||||
|
||||
const { id: _id, ...payload } = dto as UpdateEquipmentDto & { id?: string };
|
||||
const updated = await this.prisma.equipment.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...payload,
|
||||
dateOfInspection: dto.dateOfInspection ? new Date(dto.dateOfInspection) : undefined,
|
||||
commissionedAt: dto.commissionedAt ? new Date(dto.commissionedAt) : undefined,
|
||||
},
|
||||
});
|
||||
|
||||
return this.toRecord(updated);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
await this.findOne(id);
|
||||
const deleted = await this.prisma.equipment.delete({ where: { id } });
|
||||
return this.toRecord(deleted);
|
||||
}
|
||||
|
||||
private toRecord(item: {
|
||||
id: string;
|
||||
name: string;
|
||||
serialNumber: string;
|
||||
dateOfInspection: Date | null;
|
||||
commissionedAt: Date | null;
|
||||
status: EquipmentStatus;
|
||||
}) {
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
serialNumber: item.serialNumber,
|
||||
dateOfInspection: item.dateOfInspection?.toISOString() ?? null,
|
||||
commissionedAt: item.commissionedAt?.toISOString() ?? null,
|
||||
status: item.status,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user