2025-01-27 20:44:27 +00:00
|
|
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
|
|
|
import { Address } from 'src/domain/models/address.model';
|
|
|
|
|
import { Connection } from 'typeorm';
|
|
|
|
|
import { Pcclientendent } from '../../domain/entity/tables/pcclientendent.entity';
|
|
|
|
|
import { Pccidade } from '../../domain/entity/tables/pccidade.entity';
|
|
|
|
|
import { ResultModel } from 'src/domain/models/result.model';
|
|
|
|
|
import { connectionOptions } from 'src/configs/typeorm.config';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class AddressCustomerService {
|
|
|
|
|
|
|
|
|
|
async getAdresses(idCustomer: number) {
|
|
|
|
|
const connection = new Connection(connectionOptions);
|
|
|
|
|
await connection.connect();
|
|
|
|
|
const queryRunner = connection.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
try {
|
|
|
|
|
const addresses = await queryRunner.manager
|
|
|
|
|
.getRepository(Pcclientendent)
|
|
|
|
|
.createQueryBuilder('pcclientendent')
|
|
|
|
|
.select('\"pcclientendent\".codcli', 'customerId')
|
|
|
|
|
.addSelect('\"pcclientendent\".codendentcli', 'idAddress')
|
|
|
|
|
.addSelect('\"pcclientendent\".bairroent', 'neighbourhood')
|
|
|
|
|
.addSelect('\"pcclientendent\".municent', 'city')
|
|
|
|
|
.addSelect('\"pcclientendent\".estent', 'state')
|
|
|
|
|
.addSelect('\"pcclientendent\".cepent', 'zipCode')
|
|
|
|
|
.addSelect('\"pcclientendent\".enderent', 'street')
|
|
|
|
|
.addSelect('\"pcclientendent\".complementoent', 'complement')
|
|
|
|
|
.addSelect('\"pcclientendent\".numeroent', 'numberAddress')
|
|
|
|
|
.addSelect('\"pcclientendent\".codcidade', 'cityCode')
|
|
|
|
|
.addSelect('\"pcclientendent\".pontorefer', 'referencePoint')
|
|
|
|
|
.addSelect('\"pcclientendent\".observacao', 'note')
|
|
|
|
|
.addSelect('\"pcclientendent\".telent', 'phone')
|
|
|
|
|
.addSelect('\"pcclientendent\".telent', 'cellPhone')
|
|
|
|
|
.addSelect('\"pcclientendent\".codpracaent', 'placeId')
|
|
|
|
|
.addSelect('\"pcclientendent\".latitude', 'latitude')
|
|
|
|
|
.addSelect('\"pcclientendent\".longitude', 'longitude')
|
|
|
|
|
.where("\"pcclientendent\".codcli = :idCustomer", { idCustomer })
|
|
|
|
|
.getRawMany();
|
|
|
|
|
return new ResultModel(true, null, addresses, null);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// throw new HttpException(new ResultModel(false, error.status == 404 ?
|
|
|
|
|
// 'Não foram encontrados endereços de entrega para este cliente.' :
|
|
|
|
|
// error.message, {}, error),
|
|
|
|
|
// error.status == 404 ? error.status : HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
// console.log(error);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
await connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getAddress(idCustomer: number, idAddress: number) {
|
|
|
|
|
const connection = new Connection(connectionOptions);
|
|
|
|
|
await connection.connect();
|
|
|
|
|
const queryRunner = connection.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
console.log('getAddress');
|
|
|
|
|
try {
|
|
|
|
|
const sql = 'SELECT ' +
|
|
|
|
|
' pcclientendent.codcli as "customerId" ' +
|
|
|
|
|
' ,pcclientendent.codendentcli as "idAddress" ' +
|
|
|
|
|
' ,pcclientendent.bairroent as "neighbourhood" ' +
|
|
|
|
|
' ,pcclientendent.municent as "city" ' +
|
|
|
|
|
' ,pcclientendent.estent as "state" ' +
|
|
|
|
|
' ,pcclientendent.cepent as "zipCode" ' +
|
|
|
|
|
' ,pcclientendent.enderent as "street" ' +
|
|
|
|
|
' ,pcclientendent.complementoent as "complement" ' +
|
|
|
|
|
' ,pcclientendent.numeroent as "numberAddress" ' +
|
|
|
|
|
' ,pcclientendent.codcidade as "cityCode" ' +
|
|
|
|
|
' ,pcclientendent.pontorefer as "referencePoint" ' +
|
|
|
|
|
' ,pcclientendent.observacao as "note" ' +
|
|
|
|
|
' ,pcclientendent.telent as "phone" ' +
|
|
|
|
|
' ,pcclientendent.telent as "cellPhone" ' +
|
|
|
|
|
' ,pcclientendent.codpracaent as "placeId" ' +
|
|
|
|
|
' ,pcclientendent.latitude as "latitude" ' +
|
|
|
|
|
' ,pcclientendent.longitude as "longitude" ' +
|
|
|
|
|
' FROM pcclientendent ' +
|
|
|
|
|
' WHERE pcclientendent.codcli = :idCustomer ' +
|
|
|
|
|
' AND pcclientendent.codendentcli = :idAddress ';
|
|
|
|
|
|
|
|
|
|
const address = await queryRunner.query( sql, [idCustomer, idAddress] ) ;
|
|
|
|
|
|
2025-03-14 12:26:28 +00:00
|
|
|
|
2025-01-27 20:44:27 +00:00
|
|
|
return address[0];
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
await connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getAddressByCep(idCustomer: number, cep: string ) {
|
|
|
|
|
const connection = new Connection(connectionOptions);
|
|
|
|
|
await connection.connect();
|
|
|
|
|
const queryRunner = connection.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const sql = 'SELECT ' +
|
|
|
|
|
' pcclientendent.codcli as "customerId" ' +
|
|
|
|
|
' ,pcclientendent.codendentcli as "idAddress" ' +
|
|
|
|
|
' ,pcclientendent.bairroent as "neighbourhood" ' +
|
|
|
|
|
' ,pcclientendent.municent as "city" ' +
|
|
|
|
|
' ,pcclientendent.estent as "state" ' +
|
|
|
|
|
' ,pcclientendent.cepent as "zipCode" ' +
|
|
|
|
|
' ,pcclientendent.enderent as "street" ' +
|
|
|
|
|
' ,pcclientendent.complementoent as "complement" ' +
|
|
|
|
|
' ,pcclientendent.numeroent as "numberAddress" ' +
|
|
|
|
|
' ,pcclientendent.codcidade as "cityCode" ' +
|
|
|
|
|
' ,pcclientendent.pontorefer as "referencePoint" ' +
|
|
|
|
|
' ,pcclientendent.observacao as "note" ' +
|
|
|
|
|
' ,pcclientendent.telent as "phone" ' +
|
|
|
|
|
' ,pcclientendent.telent as "cellPhone" ' +
|
|
|
|
|
' ,pcclientendent.codpracaent as "placeId" ' +
|
|
|
|
|
' ,pcclientendent.latitude as "latitude" ' +
|
|
|
|
|
' ,pcclientendent.longitude as "longitude" ' +
|
|
|
|
|
' FROM pcclientendent ' +
|
|
|
|
|
' WHERE pcclientendent.codcli = :idCustomer ' +
|
|
|
|
|
" AND REGEXP_REPLACE(pcclientendent.cepent, '[^0-9]', '') = REGEXP_REPLACE(:cepent, '[^0-9]', '')";
|
|
|
|
|
|
|
|
|
|
const address = await queryRunner.query( sql, [idCustomer, cep]);
|
|
|
|
|
return address[0];
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
await connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateAddress(data: Address) {
|
|
|
|
|
|
|
|
|
|
let address = await this.getAddress(data.idCustomer, data.idAddress);
|
|
|
|
|
if (address == null) {
|
|
|
|
|
throw new HttpException('Endereço não encontrado para alteração.', HttpStatus.NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cityCode = await this.findCity(data.ibgeCode);
|
|
|
|
|
const connection = new Connection(connectionOptions);
|
|
|
|
|
await connection.connect();
|
|
|
|
|
const queryRunner = connection.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
try {
|
|
|
|
|
console.log(data);
|
|
|
|
|
const customer = await queryRunner.query('SELECT PCCLIENT.CODCLI as "customerId", PCCLIENT.CLIENTE as "name", PCCLIENT.CEPENT as "zipCode", ' +
|
|
|
|
|
' PCPRACA.NUMREGIAO as "region", PCCLIENT.EMAIL as "email" ' +
|
|
|
|
|
' FROM PCCLIENT, PCPRACA ' +
|
|
|
|
|
' WHERE PCCLIENT.CODPRACA = PCPRACA.CODPRACA (+) ' +
|
|
|
|
|
' AND PCCLIENT.CODCLI = :1', [data.idCustomer]);
|
|
|
|
|
console.log("cliente:" + JSON.stringify(customer));
|
|
|
|
|
await queryRunner.manager
|
|
|
|
|
.createQueryBuilder()
|
|
|
|
|
.update(Pcclientendent)
|
|
|
|
|
.set({
|
|
|
|
|
enderent: data.street,
|
|
|
|
|
numeroent: data.numberAddress,
|
|
|
|
|
bairroent: data.neighbourhood,
|
|
|
|
|
complementoent: data.complement,
|
|
|
|
|
municent: data.city,
|
|
|
|
|
estent: data.state,
|
|
|
|
|
cepent: data.zipCode,
|
|
|
|
|
observacao: data.note,
|
|
|
|
|
telent: data.cellPhone,
|
|
|
|
|
fonerecebedor: data.phone,
|
|
|
|
|
codmunicipio: cityCode,
|
|
|
|
|
codcidade: cityCode,
|
|
|
|
|
codpracaent: data.placeId,
|
|
|
|
|
pontorefer: data.referencePoint,
|
|
|
|
|
razaorecebedor: customer[0].name,
|
|
|
|
|
fantasia: customer[0].name,
|
|
|
|
|
ceprecebedor: customer[0].zipCode,
|
|
|
|
|
numregiao: customer[0].region,
|
|
|
|
|
codpaisrecebedor: 1058,
|
|
|
|
|
emailRecebedor: customer[0].email,
|
|
|
|
|
latitude: ( data.latitude ) ? data.latitude.toString() : '0',
|
|
|
|
|
longitude:( data.longitude ) ? data.longitude.toString() : '0',
|
|
|
|
|
})
|
|
|
|
|
.where("\"PCCLIENTENDENT\".codcli = :codcli and \"PCCLIENTENDENT\".codendentcli = :codendentcli",
|
|
|
|
|
{ codcli: data.idCustomer, codendentcli: data.idAddress })
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
address = await this.getAddress(data.idCustomer, data.idAddress);
|
|
|
|
|
|
|
|
|
|
return address;
|
|
|
|
|
|
|
|
|
|
} catch (erro) {
|
|
|
|
|
console.log(erro);
|
|
|
|
|
throw new HttpException("Erro ao criar item no carrinho de compras", HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
await connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createAddress(data: Address) {
|
|
|
|
|
console.log(data);
|
|
|
|
|
const address = await this.getAddressByCep(data.idCustomer, data.zipCode);
|
|
|
|
|
if ( address != null ) {
|
|
|
|
|
data.idAddress = address.idAddress;
|
|
|
|
|
const result = this.updateAddress(data);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
const connection = new Connection(connectionOptions);
|
|
|
|
|
await connection.connect();
|
|
|
|
|
const queryRunner = connection.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const cityCode = await this.findCity(data.ibgeCode);
|
|
|
|
|
const customer = await queryRunner.query('SELECT PCCLIENT.CODCLI as "customerId", PCCLIENT.CLIENTE as "name", PCCLIENT.CEPENT as "zipCode", ' +
|
|
|
|
|
' PCPRACA.NUMREGIAO as "region", PCCLIENT.EMAIL as "email" ' +
|
|
|
|
|
' FROM PCCLIENT, PCPRACA ' +
|
|
|
|
|
' WHERE PCCLIENT.CODPRACA = PCPRACA.CODPRACA (+) ' +
|
|
|
|
|
' AND PCCLIENT.CODCLI = :1', [data.idCustomer]);
|
|
|
|
|
|
|
|
|
|
const id = await this.getIdAddress();
|
|
|
|
|
const newPcclientendent = new Pcclientendent();
|
|
|
|
|
newPcclientendent.codendentcli = id;
|
|
|
|
|
newPcclientendent.codcli = data.idCustomer;
|
|
|
|
|
newPcclientendent.enderent = data.street;
|
|
|
|
|
newPcclientendent.numeroent = data.numberAddress;
|
|
|
|
|
newPcclientendent.complementoent = data.complement;
|
|
|
|
|
newPcclientendent.bairroent = data.neighbourhood;
|
|
|
|
|
newPcclientendent.municent = data.city;
|
|
|
|
|
newPcclientendent.estent = data.state;
|
|
|
|
|
newPcclientendent.cepent = data.zipCode;
|
|
|
|
|
newPcclientendent.observacao = data.note;
|
|
|
|
|
newPcclientendent.fonerecebedor = data.phone;
|
|
|
|
|
newPcclientendent.telent = data.cellPhone;
|
|
|
|
|
newPcclientendent.codmunicipio = Number.parseInt(data.ibgeCode);
|
|
|
|
|
newPcclientendent.codcidade = cityCode;
|
|
|
|
|
newPcclientendent.pontorefer = data.referencePoint;
|
|
|
|
|
newPcclientendent.observacao = data.note;
|
|
|
|
|
newPcclientendent.codpracaent = data.placeId;
|
|
|
|
|
|
|
|
|
|
newPcclientendent.razaorecebedor = customer.name;
|
|
|
|
|
newPcclientendent.fantasia = customer.name;
|
|
|
|
|
newPcclientendent.ceprecebedor = customer.cep;
|
|
|
|
|
newPcclientendent.numregiao = customer.region;
|
|
|
|
|
newPcclientendent.codpaisrecebedor = 1058;
|
|
|
|
|
newPcclientendent.emailRecebedor = customer.email;
|
|
|
|
|
newPcclientendent.latitude = ( data.latitude ) ? data.latitude.toString() : '0';
|
|
|
|
|
newPcclientendent.longitude = ( data.longitude ) ? data.longitude.toString() : '0';
|
|
|
|
|
|
|
|
|
|
await queryRunner.manager
|
|
|
|
|
.createQueryBuilder()
|
|
|
|
|
.insert()
|
|
|
|
|
.into(Pcclientendent)
|
|
|
|
|
.values(newPcclientendent)
|
|
|
|
|
.execute();
|
|
|
|
|
const newAddress: Address = {
|
|
|
|
|
idCustomer: data.idCustomer,
|
|
|
|
|
idAddress: id,
|
|
|
|
|
neighbourhood: data.neighbourhood,
|
|
|
|
|
city: data.city,
|
|
|
|
|
state: data.state,
|
|
|
|
|
zipCode: data.zipCode,
|
|
|
|
|
street: data.street,
|
|
|
|
|
complement: data.complement,
|
|
|
|
|
numberAddress: data.numberAddress,
|
|
|
|
|
cityCode: data.cityCode,
|
|
|
|
|
referencePoint: data.referencePoint,
|
|
|
|
|
note: data.note,
|
|
|
|
|
ibgeCode: data.ibgeCode,
|
|
|
|
|
phone: data.phone,
|
|
|
|
|
cellPhone: data.cellPhone,
|
|
|
|
|
placeId: data.placeId,
|
|
|
|
|
razaorecebedor: customer.name,
|
|
|
|
|
fantasia: customer.name,
|
|
|
|
|
ceprecebedor: customer.cep,
|
|
|
|
|
numregiao: customer.region,
|
|
|
|
|
codpaisrecebedor: 1058,
|
|
|
|
|
emailRecebedor: customer.email,
|
|
|
|
|
latitude: data.latitude,
|
|
|
|
|
longitude: data.longitude,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newAddress;
|
|
|
|
|
|
|
|
|
|
} catch (erro) {
|
|
|
|
|
console.log(erro);
|
|
|
|
|
throw new HttpException("Erro ao criar endereço de entrega do cliente", HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
await connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findCity(ibgeCode: string) {
|
|
|
|
|
const connection = new Connection(connectionOptions);
|
|
|
|
|
await connection.connect();
|
|
|
|
|
const queryRunner = connection.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
try {
|
|
|
|
|
const city = await queryRunner.manager
|
|
|
|
|
.getRepository(Pccidade)
|
|
|
|
|
.createQueryBuilder('pccidade')
|
|
|
|
|
.where("\"pccidade\".CODIBGE = :codibge", { codibge: ibgeCode })
|
|
|
|
|
.getOne();
|
|
|
|
|
if (city == null)
|
|
|
|
|
return 0;
|
|
|
|
|
return city.codcidade;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
await connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getIdAddress() {
|
|
|
|
|
const connection = new Connection(connectionOptions);
|
|
|
|
|
await connection.connect();
|
|
|
|
|
const queryRunner = connection.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
try {
|
|
|
|
|
const address = await queryRunner.manager
|
|
|
|
|
.getRepository(Pcclientendent)
|
|
|
|
|
.createQueryBuilder('pcclientendent')
|
|
|
|
|
.select('max(codendentcli)', 'id')
|
|
|
|
|
.getRawOne();
|
|
|
|
|
const id = address.id + 1;
|
|
|
|
|
return id;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
await connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|