src/settings/settings.service.ts
Methods |
|
constructor(settingsRepository: Repository<Settings>, listSuppliersRepository: Repository<ListSupplier>, listDistributorsRepository: Repository
|
|||||||||||||||||||||
|
Defined in src/settings/settings.service.ts:14
|
|||||||||||||||||||||
|
Parameters :
|
| Async addDistributorToException |
addDistributorToException(id: number, distributorId: number)
|
|
Defined in src/settings/settings.service.ts:170
|
|
Returns :
unknown
|
| Async addDistributorToList |
addDistributorToList(id: number, distributorId: number)
|
|
Defined in src/settings/settings.service.ts:112
|
|
Returns :
unknown
|
| Async addSupplierToException |
addSupplierToException(id: number, supplierId: number)
|
|
Defined in src/settings/settings.service.ts:155
|
|
Returns :
unknown
|
| Async addSupplierToList |
addSupplierToList(id: number, supplierId: number)
|
|
Defined in src/settings/settings.service.ts:97
|
|
Returns :
unknown
|
| Async create | ||||||
create(body: CreateSettingsBody)
|
||||||
|
Defined in src/settings/settings.service.ts:46
|
||||||
|
Parameters :
Returns :
unknown
|
| Async exceptionDistributorsById | ||||||
exceptionDistributorsById(id: number)
|
||||||
|
Defined in src/settings/settings.service.ts:149
|
||||||
|
Parameters :
Returns :
unknown
|
| Async exceptionSuppliersById | ||||||
exceptionSuppliersById(id: number)
|
||||||
|
Defined in src/settings/settings.service.ts:141
|
||||||
|
Parameters :
Returns :
unknown
|
| Async getSettingsByClientId | ||||||
getSettingsByClientId(clientId: number)
|
||||||
|
Defined in src/settings/settings.service.ts:36
|
||||||
|
Parameters :
Returns :
unknown
|
| Async getSettingsById | ||||||
getSettingsById(id: number)
|
||||||
|
Defined in src/settings/settings.service.ts:30
|
||||||
|
Parameters :
Returns :
unknown
|
| Async listDistributorsById | ||||||
listDistributorsById(id: number)
|
||||||
|
Defined in src/settings/settings.service.ts:91
|
||||||
|
Parameters :
Returns :
unknown
|
| Async listSuppliersById | ||||||
listSuppliersById(id: number)
|
||||||
|
Defined in src/settings/settings.service.ts:83
|
||||||
|
Parameters :
Returns :
unknown
|
| Async removeDistributorFromException |
removeDistributorFromException(id: number, distributorId: number)
|
|
Defined in src/settings/settings.service.ts:192
|
|
Returns :
unknown
|
| Async removeDistributorFromList |
removeDistributorFromList(id: number, distributorId: number)
|
|
Defined in src/settings/settings.service.ts:134
|
|
Returns :
unknown
|
| Async removeSupplierFromException |
removeSupplierFromException(id: number, supplierId: number)
|
|
Defined in src/settings/settings.service.ts:185
|
|
Returns :
unknown
|
| Async removeSupplierFromList |
removeSupplierFromList(id: number, supplierId: number)
|
|
Defined in src/settings/settings.service.ts:127
|
|
Returns :
unknown
|
| Async update | ||||||
update(body: UpdateSettingsBody)
|
||||||
|
Defined in src/settings/settings.service.ts:67
|
||||||
|
Parameters :
Returns :
unknown
|
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Client } from "src/entities/client";
import { ExceptionDistributor } from "src/entities/exception-distributor";
import { ExceptionSupplier } from "src/entities/exception-supplier";
import { ListDistributor } from "src/entities/list-distributor";
import { ListSupplier } from "src/entities/list-supplier";
import { Settings } from "src/entities/settings";
import { GeneralException } from "src/util";
import { Repository } from "typeorm";
import { CreateSettingsBody, UpdateSettingsBody } from "./dto/settings.dto";
@Injectable()
export class SettingsService {
constructor(
@InjectRepository(Settings, 'settings')
private readonly settingsRepository: Repository<Settings>,
@InjectRepository(ListSupplier, 'settings')
private readonly listSuppliersRepository: Repository<ListSupplier>,
@InjectRepository(ListDistributor, 'settings')
private readonly listDistributorsRepository: Repository<ListDistributor>,
@InjectRepository(ExceptionSupplier, 'settings')
private readonly exceptionSuppliersRepository: Repository<ExceptionSupplier>,
@InjectRepository(ExceptionDistributor, 'settings')
private readonly exceptionDistributorsRepository: Repository<ExceptionDistributor>,
@InjectRepository(Client)
private readonly clientRepository: Repository<Client>,
) {}
async getSettingsById(id: number) {
return await this.settingsRepository.findOne({
where: { cdConfiguracao: id }
})
}
async getSettingsByClientId(clientId: number) {
const client = await this.clientRepository.findOneBy({ id: clientId });
if (!client) throw new NotFoundException({ messageKey: "client_not_found" });
if (!client.mtrixCode || !client.type) throw new NotFoundException({ messageKey: "invalid_client" });
return await this.settingsRepository.findOne({
where: { cdPessoa: client.mtrixCode }
})
}
async create(body: CreateSettingsBody) {
const client = await this.clientRepository.findOneBy({ id: body.clientId });
if (!client) throw new NotFoundException({ messageKey: "client_not_found" });
if (!client.mtrixCode || !client.type) throw new NotFoundException({ messageKey: "invalid_client" });
const findSettings = await this.settingsRepository.findOne({
where: {
nmConfiguracao: body.name,
}
})
if (findSettings) throw new GeneralException('settings_already_exists');
return await this.settingsRepository.save({
hmsdb: body.hmsdb,
nmConfiguracao: body.name,
cdPessoa: client.mtrixCode,
tpConfiguracao: body.type,
warehouseLocation: body.warehouseLocation
})
}
async update(body: UpdateSettingsBody) {
const settings = await this.settingsRepository.findOneBy({ cdConfiguracao: body.cdConfiguracao });
if (!settings) throw new NotFoundException({ messageKey: "settings_not_found" });
return await this.settingsRepository.update(body.cdConfiguracao, {
...settings,
workloadId: body.workloadId,
ambiente: body.ambiente,
hmsdb: body.hmsdb,
nmConfiguracao: body.name,
tpConfiguracao: body.type,
warehouseLocation: body.warehouseLocation,
dtCriacaoAmbiente: body.ambiente === 'S' ? new Date() : settings.dtCriacaoAmbiente
})
}
async listSuppliersById(id: number) {
const response = await this.listSuppliersRepository.find({
where: { cdConfiguracao: id }
})
return response
}
async listDistributorsById(id: number) {
return await this.listDistributorsRepository.find({
where: { cdConfiguracao: id }
})
}
async addSupplierToList(id: number, supplierId: number) {
const alreadyExists = await this.listSuppliersRepository.findOne({
where: {
cdConfiguracao: id,
cdFornecedor: supplierId
}
})
if (alreadyExists) throw new BadRequestException({ messageKey: "toasts.already_exists_relationship" });
return await this.listSuppliersRepository.save({
cdConfiguracao: id,
cdFornecedor: supplierId
})
}
async addDistributorToList(id: number, distributorId: number) {
const alreadyExists = await this.listDistributorsRepository.findOne({
where: {
cdConfiguracao: id,
cdIntermediario: distributorId
}
})
if (alreadyExists) throw new BadRequestException({ messageKey: "toasts.already_exists_relationship" });
return await this.listDistributorsRepository.save({
cdConfiguracao: id,
cdIntermediario: distributorId
})
}
async removeSupplierFromList(id: number, supplierId: number) {
return await this.listSuppliersRepository.delete({
cdConfiguracao: id,
cdFornecedor: supplierId
})
}
async removeDistributorFromList(id: number, distributorId: number) {
return await this.listDistributorsRepository.delete({
cdConfiguracao: id,
cdIntermediario: distributorId
})
}
async exceptionSuppliersById(id: number) {
return await this.exceptionSuppliersRepository.find({
where: { cdConfiguracao: id }
})
}
async exceptionDistributorsById(id: number) {
return await this.exceptionDistributorsRepository.find({
where: { cdConfiguracao: id }
})
}
async addSupplierToException(id: number, supplierId: number) {
const alreadyExists = await this.exceptionSuppliersRepository.findOne({
where: {
cdConfiguracao: id,
cdFornecedor: supplierId
}
})
if (alreadyExists) throw new BadRequestException({ messageKey: "toasts.already_exists_relationship" });
return await this.exceptionSuppliersRepository.save({
cdConfiguracao: id,
cdFornecedor: supplierId
})
}
async addDistributorToException(id: number, distributorId: number) {
const alreadyExists = await this.exceptionDistributorsRepository.findOne({
where: {
cdConfiguracao: id,
cdIntermediario: distributorId
}
})
if (alreadyExists) throw new BadRequestException({ messageKey: "toasts.already_exists_relationship" });
return await this.exceptionDistributorsRepository.save({
cdConfiguracao: id,
cdIntermediario: distributorId
})
}
async removeSupplierFromException(id: number, supplierId: number) {
return await this.exceptionSuppliersRepository.delete({
cdConfiguracao: id,
cdFornecedor: supplierId
})
}
async removeDistributorFromException(id: number, distributorId: number) {
return await this.exceptionDistributorsRepository.delete({
cdConfiguracao: id,
cdIntermediario: distributorId
})
}
}