import { Injectable, InternalServerErrorException, NotFoundException } from "@nestjs/common"; import { v4 as uuidv4 } from 'uuid'; import { InjectModel } from "@nestjs/mongoose"; import { Model} from 'mongoose'; import { Customer } from "../interface/customer.interface"; import { CreateCustomerDTO } from "../dto/customer.dto"; @Injectable() export class CustomerService { constructor(@InjectModel('Customer') private readonly customerModel: Model) { } public async listCustomer(): Promise { return await this.customerModel.find({}); } public async createCustomer(customer : CreateCustomerDTO): Promise { const newCustomer = await new this.customerModel(customer) return newCustomer.save(); } public async updateCustomer(id, customerDto: Partial): Promise { const updatedCustomer = await this.customerModel .findByIdAndUpdate(id, customerDto, { new: true }); return updatedCustomer; } public async getCustomer(id: string): Promise { const customer = await this.customerModel.findById(id).exec(); if(!customer){ throw new NotFoundException('customer not found'); } return customer['']; } public async removeCustomer(id: string): Promise { try { return await this.customerModel.findByIdAndRemove(id)['']; } catch(err){ throw new InternalServerErrorException(err); } } }