This repository has been archived on 2026-04-01. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Inventory_backend/src/composants/composants.service.ts

113 lines
3.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import {
CreateComposantDto,
UpdateComposantDto,
} from '../shared/dto/composant.dto';
import {
COMPONENT_WITH_RELATIONS_INCLUDE,
ComposantWithRelations,
} from '../common/constants/component-includes';
@Injectable()
export class ComposantsService {
constructor(private prisma: PrismaService) {}
private buildCreateInput(
createComposantDto: CreateComposantDto,
): Prisma.ComposantCreateInput {
const data: Prisma.ComposantCreateInput = {
name: createComposantDto.name,
reference: createComposantDto.reference ?? null,
prix:
createComposantDto.prix !== undefined ? createComposantDto.prix : null,
};
if (createComposantDto.constructeurId) {
data.constructeur = {
connect: { id: createComposantDto.constructeurId },
};
}
if (createComposantDto.typeComposantId) {
data.typeComposant = {
connect: { id: createComposantDto.typeComposantId },
};
}
if (createComposantDto.structure !== undefined) {
data.structure = createComposantDto.structure as Prisma.InputJsonValue;
}
return data;
}
async create(createComposantDto: CreateComposantDto) {
const created = await this.prisma.composant.create({
data: this.buildCreateInput(createComposantDto),
include: COMPONENT_WITH_RELATIONS_INCLUDE,
});
return created as ComposantWithRelations;
}
async findAll() {
return (await this.prisma.composant.findMany({
include: COMPONENT_WITH_RELATIONS_INCLUDE,
orderBy: { name: 'asc' },
})) as ComposantWithRelations[];
}
async findOne(id: string) {
return (await this.prisma.composant.findUnique({
where: { id },
include: COMPONENT_WITH_RELATIONS_INCLUDE,
})) as ComposantWithRelations | null;
}
async update(id: string, updateComposantDto: UpdateComposantDto) {
const data: Prisma.ComposantUpdateInput = {};
if (updateComposantDto.name !== undefined) {
data.name = updateComposantDto.name;
}
if (updateComposantDto.reference !== undefined) {
data.reference = updateComposantDto.reference;
}
if (updateComposantDto.prix !== undefined) {
data.prix = updateComposantDto.prix;
}
if (updateComposantDto.constructeurId !== undefined) {
data.constructeur = updateComposantDto.constructeurId
? { connect: { id: updateComposantDto.constructeurId } }
: { disconnect: true };
}
if (updateComposantDto.typeComposantId !== undefined) {
data.typeComposant = updateComposantDto.typeComposantId
? { connect: { id: updateComposantDto.typeComposantId } }
: { disconnect: true };
}
if (updateComposantDto.structure !== undefined) {
data.structure = updateComposantDto.structure as Prisma.InputJsonValue;
}
return (await this.prisma.composant.update({
where: { id },
data,
include: COMPONENT_WITH_RELATIONS_INCLUDE,
})) as ComposantWithRelations;
}
async remove(id: string) {
return this.prisma.composant.delete({
where: { id },
});
}
}