Skip to content

Commit

Permalink
Fixed the alcohol elimination calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
VarMattew committed Jul 22, 2024
1 parent 5775438 commit f614950
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions apps/backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,19 @@ export class UsersService {
include: { drink: { select: { alcoholContent: true } } },
});

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

for (const drinkAction of drinkActions) {
const dose = drinkAction.milliliter * (drinkAction.drink.alcoholContent / 100) * 0.789;
totalDose += dose;
}

totalDose = Math.max(0, totalDose);
const bac = (totalDose / (userWeightInGrams * genderFactor)) * 100;

return { alcoholContent: Math.max(0, bac - totalEliminatedAlcohol) };
}
const timeDifferenceMs = currentTime.getTime() - drinkAction.createdAt.getTime();
const eliminated = (timeDifferenceMs / (1000 * 60 * 60)) * 0.016;

private calculateHoursSinceFirstDrink(drinkActions: any[], currentTime: Date): number {
if (drinkActions.length === 0) {
return 0;
const bac = (dose / (userWeightInGrams * genderFactor)) * 100;
totalBac += Math.max(0, bac - eliminated);
}

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;
return { alcoholContent: Math.max(0, totalBac) };
}

async remove(authSchId: string): Promise<User> {
Expand Down

0 comments on commit f614950

Please sign in to comment.