Skip to content

Commit

Permalink
fix: show error if user is already signed in
Browse files Browse the repository at this point in the history
  • Loading branch information
Gobot1234 committed May 10, 2024
1 parent 549e6c2 commit 9115c73
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
8 changes: 8 additions & 0 deletions apps/anvil/src/sign-in/sign-in.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CheckAbilities } from "@/auth/authorization/decorators/check-abilities-decorator";
import { IsAdmin, IsRep } from "@/auth/authorization/decorators/check-roles-decorator";
import { CaslAbilityGuard } from "@/auth/authorization/guards/casl-ability.guard";
import { ErrorCodes } from "@/shared/constants/ErrorCodes";
import { IdempotencyCache } from "@/shared/decorators/idempotency.decorator";
import { User } from "@/shared/decorators/user.decorator";
import { ldapLibraryToUcardNumber } from "@/shared/functions/utils";
Expand All @@ -11,6 +12,7 @@ import { sign_in as sign_in_ } from "@ignis/types";
import type { List, Location, LocationStatus } from "@ignis/types/sign_in";
import type { User as User_ } from "@ignis/types/users";
import {
BadRequestException,
Body,
Controller,
Delete,
Expand Down Expand Up @@ -54,6 +56,12 @@ export class SignInController {
SignInController.name,
);
const user = await this.signInService.getUserForSignIn(location, ucard_number);
if (user.signed_in) {
throw new BadRequestException({
message: "User is already signed in",
code: ErrorCodes.already_signed_in_to_location,
});
}

if (user?.is_rep) {
return {
Expand Down
34 changes: 18 additions & 16 deletions apps/anvil/src/sign-in/sign-in.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from "@nestjs/common";
import { Logger } from "@nestjs/common";
import { Cron, CronExpression } from "@nestjs/schedule";
import { CardinalityViolationError, InvalidValueError } from "edgedb";
import { CardinalityViolationError, ConstraintViolationError, InvalidValueError } from "edgedb";

export const REP_ON_SHIFT = "Rep On Shift";
export const REP_OFF_SHIFT = "Rep Off Shift";
Expand Down Expand Up @@ -453,7 +453,7 @@ export class SignInService implements OnModuleInit {
}).user,
);
} catch (error) {
if (error instanceof CardinalityViolationError) {
if (error instanceof ConstraintViolationError) {
throw new BadRequestException({
message: `User ${ucard_number} already signed in`,
code: ErrorCodes.already_signed_in_to_location,
Expand Down Expand Up @@ -580,7 +580,7 @@ export class SignInService implements OnModuleInit {
}),
);
} catch (error) {
if (error instanceof CardinalityViolationError) {
if (error instanceof ConstraintViolationError) {
throw new BadRequestException({
message: `Rep ${ucard_number} already signed in`,
code: ErrorCodes.already_signed_in_to_location,
Expand Down Expand Up @@ -610,19 +610,21 @@ export class SignInService implements OnModuleInit {

try {
await this.dbService.query(
e.update(e.sign_in.SignIn, (sign_in) => {
const isCorrectLocation = e.op(sign_in.location, "=", castLocation(location));
const userMatches = e.op(sign_in.user.ucard_number, "=", ucard_number);
const doesNotExist = e.op("not", e.op("exists", sign_in.ends_at));
e.assert_exists(
e.update(e.sign_in.SignIn, (sign_in) => {
const isCorrectLocation = e.op(sign_in.location, "=", castLocation(location));
const userMatches = e.op(sign_in.user.ucard_number, "=", ucard_number);
const doesNotExist = e.op("not", e.op("exists", sign_in.ends_at));

return {
filter_single: e.all(e.set(isCorrectLocation, userMatches, doesNotExist)),
set: {
tools,
reason,
},
};
}),
return {
filter_single: e.all(e.set(isCorrectLocation, userMatches, doesNotExist)),
set: {
tools,
reason,
},
};
}),
),
);
} catch (e) {
if (e instanceof CardinalityViolationError && e.code === 84017154) {
Expand Down Expand Up @@ -746,7 +748,7 @@ export class SignInService implements OnModuleInit {
),
);
} catch (e) {
if (e instanceof CardinalityViolationError && e.code === 84017154) {
if (e instanceof ConstraintViolationError && e.code === 84017154) {
console.log(e, e.code);
throw new HttpException("The user is already in the queue", HttpStatus.BAD_REQUEST);
}
Expand Down

0 comments on commit 9115c73

Please sign in to comment.