Skip to content

Instantly share code, notes, and snippets.

@eemr3
Created October 23, 2023 21:58
Show Gist options
  • Save eemr3/1f8b72fec178ea3a22692c94a7708fb7 to your computer and use it in GitHub Desktop.
Save eemr3/1f8b72fec178ea3a22692c94a7708fb7 to your computer and use it in GitHub Desktop.
Service do DailyMoviments do APP Mariah
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../database/prisma/prisma.service';
import { UpdateDailyMovementDto } from './dto/update-daily_movement.dto';
import { CreateDailyMovementDto } from './dto/create-daily_movement.dto';
@Injectable()
export class DailyMovementsService {
constructor(private prisma: PrismaService) {}
async create(createDailyMovementDto: CreateDailyMovementDto) {
const initialDate = new Date(createDailyMovementDto.initialDate);
const finalDate = new Date(createDailyMovementDto.finalDate);
const result = await this.returnOfTheTotalValuePerMethod(
initialDate,
finalDate,
);
const dailyMovements = await this.prisma.dailyMovements.create({
data: {
totalSales: result.totalDayMoviment,
cashSales: result.cash,
pixSales: result.pix,
debitCardSales: result.debitCard,
creditCardSalesCash: result.creditCard,
creditCardsSalesInstallment: 0,
},
});
return dailyMovements;
}
findAll() {
return `This action returns all dailyMovements`;
}
async findOne(id: number) {
return `This action returns a ${id} dailyMovement`;
}
update(id: number, updateDailyMovementDto: UpdateDailyMovementDto) {
console.log(updateDailyMovementDto);
return `This action updates a #${id} dailyMovement`;
}
remove(id: number) {
return `This action removes a #${id} dailyMovement`;
}
async findAllSalesPerDayMoviment(initialDate: Date, finalDate: Date) {
return await this.prisma.sales.findMany({
where: {
createdAt: {
gte: initialDate,
lte: finalDate,
},
},
include: {
Payments: true,
},
});
}
async findDayMoviment(movimentDate: Date) {
const date = new Date(movimentDate);
const moviment = await this.prisma.dailyMovements.findFirst({
where: {
createdAt: {
gte: date,
lte: new Date(date.getTime() + 999),
},
},
});
return moviment;
}
async returnOfTheTotalValuePerMethod(initialDate: Date, finalDate: Date) {
const sales = await this.findAllSalesPerDayMoviment(initialDate, finalDate);
const pix = sales
.filter((sale) => sale.Payments.some((p) => p.method === 'pix'))
.reduce((acc, curr) => acc + curr.totalPrice, 0);
const cash = sales
.filter((sale) => sale.Payments.some((p) => p.method === 'money'))
.reduce((acc, curr) => acc + curr.totalPrice, 0);
const debitCard = sales
.filter((sale) => sale.Payments.some((p) => p.method === 'debit_card'))
.reduce((acc, curr) => acc + curr.totalPrice, 0);
const creditCard = sales
.filter((sale) => sale.Payments.some((p) => p.method === 'credit_card'))
.reduce((acc, curr) => acc + curr.totalPrice, 0);
const totalDayMoviment = pix + cash + debitCard + creditCard;
return {
pix,
cash,
debitCard,
creditCard,
totalDayMoviment,
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment