Skip to content

Commit

Permalink
Fixed the calculation and implemented alcohol elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
VarMattew committed Jul 15, 2024
1 parent 165ad56 commit 5775438
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions apps/backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,53 @@ export class UsersService {
return await this.prisma.user.findMany({ omit: { weight: true } });
}

private calculateHoursSinceDrink(drinkTime: Date, currentTime: Date): number {
const timeDifference = currentTime.getTime() - drinkTime.getTime();
return timeDifference / (1000 * 60 * 60);
}

async calBac(authSchId: string): Promise<UserBac> {
const drinkActions = await this.prisma.drinkAction.findMany({
// where: { authSchId },
include: { drink: { select: { alcoholContent: true } } },
const user = await this.prisma.user.findUnique({
where: { authSchId },
select: { weight: true, gender: true },
});

if (!user) {
throw new NotFoundException('User not found');
}

const currentTime = new Date();
const userWeightInGrams = user.weight * 1000;
const genderFactor = user.gender === 'Male' ? 0.68 : 0.55;

const user = await this.prisma.user.findUnique({ where: { authSchId }, select: { weight: true, gender: true } });
const drinkActions = await this.prisma.drinkAction.findMany({
// where: { authSchId },
include: { drink: { select: { alcoholContent: true } } },
});

let totalDose = 0;
let totalEliminatedAlcohol = 0;
const totalEliminatedAlcohol = this.calculateHoursSinceFirstDrink(drinkActions, currentTime) * 0.016;

for (const drinkAction of drinkActions) {
const dose = drinkAction.milliliter * drinkAction.drink.alcoholContent * 0.789;
const hoursSinceDrink = this.calculateHoursSinceDrink(drinkAction.createdAt, currentTime);
const eliminatedAlcohol = hoursSinceDrink * 0.016;
const dose = drinkAction.milliliter * (drinkAction.drink.alcoholContent / 100) * 0.789;
totalDose += dose;
totalEliminatedAlcohol += eliminatedAlcohol;
}

const userWeightInGrams = user.weight * 1000;
const genderFactor = user.gender === 'Male' ? 0.68 : 0.55;
totalDose = Math.max(0, totalDose);
const bac = (totalDose / (userWeightInGrams * genderFactor)) * 100;

return { alcoholContent: Math.max(0, bac - totalEliminatedAlcohol) };
}

private calculateHoursSinceFirstDrink(drinkActions: any[], currentTime: Date): number {
if (drinkActions.length === 0) {
return 0;
}

drinkActions.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());

const firstDrinkTime = drinkActions[0].createdAt;
const timeDifferenceMs = currentTime.getTime() - firstDrinkTime.getTime();
const hoursDifference = timeDifferenceMs / (1000 * 60 * 60);

return hoursDifference;
}

async remove(authSchId: string): Promise<User> {
try {
return await this.prisma.user.delete({ where: { authSchId } });
Expand Down

0 comments on commit 5775438

Please sign in to comment.