Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

25 bac query endpoint #31

Merged
merged 10 commits into from
Aug 1, 2024
4 changes: 2 additions & 2 deletions apps/backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export class UsersController {
@ApiBearerAuth()
@ApiOperation({ summary: 'Get BAC (Blood Alcohol Content)' })
async calculateBloodAlcoholContent(
@Param('id', ParseUUIDPipe) id: string,
// @Param('id', ParseUUIDPipe) id: string,
@CurrentUser() user: User
): Promise<UserBac> {
VarMattew marked this conversation as resolved.
Show resolved Hide resolved
return await this.usersService.calculateBloodAlcoholContent(id, user);
return await this.usersService.calculateBloodAlcoholContent(user);
}

@Delete(':id')
Expand Down
23 changes: 13 additions & 10 deletions apps/backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ export class UsersService {
return await this.prisma.user.findMany({ omit: { weight: true } });
}

async calculateBloodAlcoholContent(authSchId: string, user: User): Promise<UserBac> {
if (!user.weight || !user.gender) {
async calculateBloodAlcoholContent(user: User): Promise<UserBac> {
if (!user.weight) {
throw new NotFoundException('User has no weight or gender set');
}

if (!user.gender) {
throw new NotFoundException('User has no weight or gender set');
}

Expand All @@ -48,7 +52,7 @@ export class UsersService {
const genderFactor = user.gender === 'Male' ? 0.68 : 0.55;

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

Expand All @@ -57,23 +61,22 @@ export class UsersService {
for (const drinkAction of drinkActions) {
if (drinkAction.hasEffect) {
const dose = drinkAction.milliliter * (drinkAction.drink.alcoholContent / 100) * 0.789;

const timeDifferenceMs = currentTime.getTime() - drinkAction.createdAt.getTime();
const eliminated = (timeDifferenceMs / (1000 * 60 * 60)) * 0.016;

const bac = Math.max(
0,
Math.round(((dose / (userWeightInGrams * genderFactor)) * 100 - eliminated) * 10000) / 10000
);
const bac = Math.max(0, (dose / (userWeightInGrams * genderFactor)) * 100 - eliminated);
if (bac === 0) {
drinkAction.hasEffect = false;
await this.prisma.drinkAction.update({
where: { id: drinkAction.id },
data: { hasEffect: false },
});
} else {
totalBac += bac;
}
}
}

return { alcoholContent: Math.max(0, totalBac) };
return { alcoholContent: Math.round(totalBac * 10000) / 10000 };
}

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