I was already finishing my application when out of nowhere my order simply didn't load, I couldn't even post it, this stayed in this infinite loop saying it was sending but it never sent.
my service:
* eslint-disable prettier/prettier */
import { Comprador, Compradores, CreateOneCompraDTO, PaginacaoDto, UpdateCompraDTO } from '@app/lib';
import { Injectable, NotFoundException, OnModuleInit } from '@nestjs/common';
//import {Compra, Compradores,CreateCompraDto,PaginacaoDto,UpdateCompraDto} from '@app/common'
import { randomUUID } from 'crypto';
import { Observable, Subject } from 'rxjs';
@Injectable()
export class DadosService implements OnModuleInit {
private readonly compradores: Comprador[] = [];
onModuleInit() {
}
create(CreateCompraDto: CreateOneCompraDTO) {
const comprador: Comprador = {
...CreateCompraDto,
dadosCompra: {},
id: randomUUID(),
}
this.compradores.push(comprador)
return comprador
}
findAll(): Compradores {
return {compradores: this.compradores}
}
findOne(id: string): any{
return this.compradores.find((comprador) => comprador.id === id)
}
updateOne(id: string, dados: UpdateCompraDTO) {
const compradorId = this.compradores.findIndex((comp) => comp.id === id)
if(compradorId !== -1){
this.compradores[compradorId] = {
...this.compradores[compradorId],
...dados
}
return this.compradores[compradorId]
}
throw new NotFoundException(`Comprador do id: ${id}`)
}
remove(id: string){
const deleteId = this.compradores.findIndex((del) => del.id === id)
if(deleteId !== -1){
return this.compradores.splice(deleteId, 1)[0]
}
throw new NotFoundException(`Comprador do id: ${id}`)
}
queryCompradores(paginacaoDTO: Observable<PaginacaoDto>): Observable<Compradores> {
const subject = new Subject<Compradores>()
const onNext = (paginacaoDTO: PaginacaoDto) => {
const start = paginacaoDTO.page * paginacaoDTO.skip
subject.next({
compradores: this.compradores.splice(start, start + paginacaoDTO.skip),
})
};
const onComplete = () => subject.complete()
paginacaoDTO.subscribe({
next: onNext,
complete: onComplete
})
return subject.asObservable();
}
}
my controler:
/* eslint-disable @typescript-eslint/no-unused-vars *//* eslint-disable prettier/prettier */
//import { Controller } from '@nestjs/common';
import { Controller } from '@nestjs/common'
import { DadosService } from './dados.service';
import { Observable } from 'rxjs';
import { GrpcMethod } from '@nestjs/microservices';
import { Comprador, Compradores, CompradosServiceController, CreateOneCompraDTO, FindOneCompraDTO, PaginacaoDto, UpdateCompraDTO } from '@app/lib';
@Controller()
export class DadosController implements CompradosServiceController {
constructor(private readonly dadosService: DadosService) {}
@GrpcMethod('CompradosService', 'queryComprador')
queryComprador(request: Observable<PaginacaoDto>): Observable<Compradores> {
return this.dadosService.queryCompradores(request)
}
@GrpcMethod('CompradosService', 'createOneCompra')
createOneCompra(CreateCompraDto: CreateOneCompraDTO): Comprador{
return this.dadosService.create(CreateCompraDto)
}
@GrpcMethod('CompradosService', 'findAllCompra')
findAllCompra() {
return this.dadosService.findAll()
}
@GrpcMethod('CompradosService', 'findOneCompra')
findOneCompra(request: FindOneCompraDTO) {
return this.dadosService.findOne(request.id)
}
@GrpcMethod('CompradosService', 'updateOneCompra')
updateOneCompra(request: UpdateCompraDTO) {
return this.dadosService.updateOne(request.id, request)
}
@GrpcMethod('CompradosService', 'deleteOneCompra')
deleteOneCompra(FindOneCompraDTO: FindOneCompraDTO) {
return this.dadosService.remove(FindOneCompraDTO.id)
}
}
my controler apigateway
/* eslint-disable prettier/prettier */
import { CreateOneCompraDTO, UpdateCompraDTO } from '@app/lib';
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { CompradoresService } from 'apps/compra/src/compradores/compradores.service';
import { HashearSenha } from '../recursos/pipeHashearSenha';
@Controller('/compradores')
export class CompradoresController {
constructor(private readonly compradoresService: CompradoresService) {}
@Post()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
criarCompradores(@Body() {senha, ...dados}: CreateOneCompraDTO, @Body('senha', HashearSenha) senhaHash: string){
return this.compradoresService.criarComprador({senha: senhaHash, ...dados})
}
@Get()
listarOsCOmpradores (){
return this.compradoresService.listarCompradores()
}
@Get('/:id')
listarOComprador(@Param('id') id: string){
return this.compradoresService.listarComprador(id)
}
@Patch('/:id')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
atualizarOComprador(@Param('id') id: string, @Body() {senha, ...dados}: UpdateCompraDTO, @Body('senha', HashearSenha) senhaHash: string){
return this.compradoresService.atualizarComprador(id, {senha: senhaHash, ...dados})
}
@Delete('/:id')
deletarOComprador(@Param('id') id: string){
return this.compradoresService.deletarComprador(id)
}
@Post('compradores')
compradoresStream(){
return this.compradoresService.Compradores()
}
}
my service apigateway
/* eslint-disable prettier/prettier */
//import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { ReplaySubject } from 'rxjs';
import { AUTH_SERVICE } from './constantes';
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { COMPRADOS_SERVICE_NAME, CompradosServiceClient, CreateOneCompraDTO, PaginacaoDto, UpdateCompraDTO } from '@app/lib';
@Injectable()
export class CompradoresService implements OnModuleInit {
private compradorService: CompradosServiceClient
constructor(
@Inject(AUTH_SERVICE) private cliente: ClientGrpc
){}
onModuleInit() {
this.compradorService = this.cliente.getService<CompradosServiceClient>(COMPRADOS_SERVICE_NAME)
}
criarComprador(dados: CreateOneCompraDTO){
return this.compradorService.createOneCompra(dados)
}
listarCompradores(){
return this.compradorService.findAllCompra({})
}
listarComprador(id: string){
return this.compradorService.findOneCompra({id})
}
atualizarComprador(id: string, dados: UpdateCompraDTO){
return this.compradorService.updateOneCompra({id, ...dados})
}
deletarComprador(id: string){
return this.compradorService.deleteOneCompra({id})
}
Compradores(){
const Compradores$ = new ReplaySubject<PaginacaoDto>()
Compradores$.next({ page: 0, skip: 200})
Compradores$.next({ page: 1, skip: 200})
Compradores$.next({ page: 2, skip: 200})
Compradores$.next({ page: 3, skip: 200})
Compradores$.complete();
let parcialNumber = 1;
this.compradorService.queryComprador(Compradores$).subscribe((empresas) => {
console.log('Chunk', parcialNumber, empresas)
parcialNumber += 1
})
}
}
when I click in send, the 'Sending Request' never ends, idk what to do, my get and delete works, but post and patch stays in this infinite loop, It was working normally, I have no idea what I did.
I found the error and it was very specific, so my password generatorHash(pipe) took a string in my .env 'SAL_SENHA', however the salt contained a size that blocked this password generator, it was 30 I think, After I generated a new sal with size 10 it worked.
if you use bcrypt like me, write NODE on terminal, next, you:
const bcrypt = require('bcrypt')
and after you write this to generate the salt:
bcrypt.genSaltSync(10)
try to not generate more than 10, that was my mistake, I generated a size 10 and everything went back to normal, Maybe it works on yours, but only 10 worked on mine