Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Add constants errors in pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 8, 2020
1 parent a7ee700 commit 91aba2e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/errors/errors.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const MFA_ENABLED_CONFLICT =
'Two-factor authentication is already enabled';
export const MFA_NOT_ENABLED = 'MFA_NOT_ENABLED';
export const MFA_BACKUP_CODE_USED = 'MFA_BACKUP_CODE_USED';
export const MFA_PHONE_OR_TOKEN_REQUIRED = 'MFA_BACKUP_CODE_USED';
export const UNVERIFIED_LOCATION = 'UNVERIFIED_LOCATION';

export const CURRENT_PASSWORD_REQUIRED = 'Current password is required';
Expand All @@ -43,3 +44,9 @@ export const CANNOT_DELETE_SOLE_MEMBER = '';
export const CANNOT_DELETE_SOLE_OWNER = '';

export const BILLING_ACCOUNT_CREATED_CONFLICT = '';

export const ORDER_BY_ASC_DESC = 'Order should be "ASC" or "DESC"';
export const ORDER_BY_FORMAT = 'Order should be "ASC" or "DESC"';
export const WHERE_PIPE_FORMAT = 'Order should be "ASC" or "DESC"';
export const OPTIONAL_INT_PIPE_NUMBER = '$key should be a number';
export const CURSOR_PIPE_FORMAT = `$key should be like "id 12, name Anand"`;
4 changes: 2 additions & 2 deletions src/interceptors/audit-log.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { auditLogsCreateInput } from '@prisma/client';
import { getClientIp } from 'request-ip';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { GROUP_NOT_FOUND } from 'src/errors/errors.constants';
import { STAART_AUDIT_LOG_DATA } from 'src/modules/audit-logs/audit-log.constants';
import { UserRequest } from 'src/modules/auth/auth.interface';
import { GeolocationService } from 'src/modules/geolocation/geolocation.service';
Expand Down Expand Up @@ -41,8 +42,7 @@ export class AuditLogger implements NestInterceptor {
if (typeof auditLog === 'string') auditLog = [auditLog];
const request = context.switchToHttp().getRequest() as UserRequest;
const groupId = parseInt(request.params.id);
if (isNaN(groupId))
throw new BadGatewayException('Group ID is not a number');
if (isNaN(groupId)) throw new BadGatewayException(GROUP_NOT_FOUND);
const ip = getClientIp(request);
const location = await this.geolocationService.getLocation(ip);
const userAgent = request.get('user-agent');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Post,
} from '@nestjs/common';
import { users } from '@prisma/client';
import { MFA_PHONE_OR_TOKEN_REQUIRED } from '../../errors/errors.constants';
import { Expose } from '../../modules/prisma/prisma.interface';
import { Scopes } from '../auth/scope.decorator';
import {
Expand Down Expand Up @@ -70,7 +71,7 @@ export class MultiFactorAuthenticationController {
userId,
body.phone,
);
throw new BadRequestException('Phone number or token is required');
throw new BadRequestException(MFA_PHONE_OR_TOKEN_REQUIRED);
}

@Post('email')
Expand Down
3 changes: 2 additions & 1 deletion src/pipes/cursor.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Injectable,
PipeTransform,
} from '@nestjs/common';
import { CURSOR_PIPE_FORMAT } from 'src/errors/errors.constants';
import { parseObjectLiteral } from '../helpers/parse-object-literal';

/** Convert a string like "id: 12, b: 'Anand'" to { id: 12, name: "Anand" } */
Expand All @@ -25,7 +26,7 @@ export class CursorPipe implements PipeTransform {
return items;
} catch (_) {
throw new BadRequestException(
`"${metadata.data}" should be like "id 12, name Anand", provided "${value}"`,
CURSOR_PIPE_FORMAT.replace('$key', metadata.data),
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/pipes/optional-int.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Injectable,
PipeTransform,
} from '@nestjs/common';
import { OPTIONAL_INT_PIPE_NUMBER } from 'src/errors/errors.constants';

/** Convert a string like "1" to a number, but without NaN */
@Injectable()
Expand All @@ -13,7 +14,7 @@ export class OptionalIntPipe implements PipeTransform {
const num = Number(value);
if (isNaN(num))
throw new BadRequestException(
`"${metadata.data}" should be a number, provided "${value}"`,
OPTIONAL_INT_PIPE_NUMBER.replace('$key', metadata.data),
);
return num;
}
Expand Down
10 changes: 6 additions & 4 deletions src/pipes/order-by.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
Injectable,
PipeTransform,
} from '@nestjs/common';
import {
ORDER_BY_ASC_DESC,
ORDER_BY_FORMAT,
} from 'src/errors/errors.constants';

/** Convert a string like "name asc, address desc" to { name: "asc", address: "desc" } */
@Injectable()
Expand All @@ -20,14 +24,12 @@ export class OrderByPipe implements PipeTransform {
rules.forEach((rule) => {
const [key, order] = rule.split(' ') as [string, 'asc' | 'desc'];
if (!['asc', 'desc'].includes(order.toLocaleLowerCase()))
throw new BadGatewayException('Order should be "ASC" or "DESC"');
throw new BadGatewayException(ORDER_BY_ASC_DESC);
orderBy[key] = order.toLocaleLowerCase() as 'asc' | 'desc';
});
return orderBy;
} catch (_) {
throw new BadRequestException(
`"${metadata.data}" should be like "key1 asc, key2 desc", provided "${value}"`,
);
throw new BadRequestException(ORDER_BY_FORMAT);
}
}
}
5 changes: 2 additions & 3 deletions src/pipes/where.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Injectable,
PipeTransform,
} from '@nestjs/common';
import { WHERE_PIPE_FORMAT } from '../errors/errors.constants';
import { parseObjectLiteral } from '../helpers/parse-object-literal';

/** Convert a string like "id: 12, b: 'Anand'" to { id: 12, name: "Anand" } */
Expand All @@ -24,9 +25,7 @@ export class WherePipe implements PipeTransform {
});
return items;
} catch (_) {
throw new BadRequestException(
`"${metadata.data}" should be like "id 12, name Anand", provided "${value}"`,
);
throw new BadRequestException(WHERE_PIPE_FORMAT);
}
}
}

0 comments on commit 91aba2e

Please sign in to comment.