Skip to content

Commit

Permalink
chore(utils): remove throwIfEmpty
Browse files Browse the repository at this point in the history
This imported a Nest thing into a shared library, so was getting bundling by the frontend - not good!
  • Loading branch information
tsa96 committed Dec 29, 2023
1 parent 94421ae commit 7c3ea67
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 38 deletions.
6 changes: 4 additions & 2 deletions apps/backend/src/app/modules/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Bitflags } from '@momentum/bitflags';
import { AdminActivityType, Role } from '@momentum/constants';
import { EXTENDED_PRISMA_SERVICE } from '../database/db.constants';
import { ExtendedPrismaService } from '../database/prisma.extension';
import { expandToIncludes, throwIfEmpty } from '@momentum/util-fn';
import { expandToIncludes, isEmpty } from '@momentum/util-fn';
import { AdminActivityService } from './admin-activity.service';

@Injectable()
Expand Down Expand Up @@ -184,7 +184,9 @@ export class AdminService {
userID: number,
update: AdminUpdateUserDto
) {
throwIfEmpty(update);
if (isEmpty(update)) {
throw new BadRequestException('Empty body');
}

const user = await this.db.user.findUnique({
where: { id: userID },
Expand Down
13 changes: 7 additions & 6 deletions apps/backend/src/app/modules/maps/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ import {
expandToIncludes,
intersection,
isEmpty,
parallel,
throwIfEmpty
parallel
} from '@momentum/util-fn';
import { File } from '@nest-lab/fastify-multer';
import { vdf } from 'fast-vdf';
Expand Down Expand Up @@ -976,8 +975,9 @@ export class MapsService {
//#region Updates

async updateAsSubmitter(mapID: number, userID: number, dto: UpdateMapDto) {
throwIfEmpty(dto);

if (isEmpty(dto)) {
throw new BadRequestException('Empty body');
}
const map = (await this.db.mMap.findUnique({
where: { id: mapID },
include: { submission: true }
Expand Down Expand Up @@ -1066,8 +1066,9 @@ export class MapsService {
* changes are done here, so a lot can happen.
*/
async updateAsAdmin(mapID: number, userID: number, dto: UpdateMapAdminDto) {
throwIfEmpty(dto);

if (isEmpty(dto)) {
throw new BadRequestException('Empty body');
}
const map = (await this.db.mMap.findUnique({
where: { id: mapID },
include: { submission: true }
Expand Down
12 changes: 4 additions & 8 deletions apps/backend/src/app/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ import {
import { Bitflags } from '@momentum/bitflags';
import { EXTENDED_PRISMA_SERVICE } from '../database/db.constants';
import { ExtendedPrismaService } from '../database/prisma.extension';
import {
expandToIncludes,
isEmpty,
throwIfEmpty,
undefinedIfEmpty
} from '@momentum/util-fn';
import { expandToIncludes, isEmpty, undefinedIfEmpty } from '@momentum/util-fn';
import { AdminActivityService } from '../admin/admin-activity.service';

@Injectable()
Expand Down Expand Up @@ -208,8 +203,9 @@ export class UsersService {
}

async update(userID: number, update: UpdateUserDto) {
throwIfEmpty(update);

if (isEmpty(update)) {
throw new BadRequestException('Empty body');
}
const user = await this.db.user.findUnique({
where: { id: userID },
include: { profile: true }
Expand Down
12 changes: 1 addition & 11 deletions libs/util-fn/src/is-empty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEmpty, throwIfEmpty, undefinedIfEmpty } from './is-empty';
import { isEmpty, undefinedIfEmpty } from './is-empty';

const testObj = { a: 1 };

Expand All @@ -13,16 +13,6 @@ describe('isEmpty', () => {
});
});

describe('throwIfEmpty', () => {
test('should throw BadRequestException if object is empty', () => {
expect(() => throwIfEmpty({})).toThrow();
});

test('should not throw BadRequestException if object is not empty', () => {
expect(() => throwIfEmpty(testObj)).not.toThrow();
});
});

describe('undefinedIfEmpty', () => {
test('should return undefined if object is empty', () => {
expect(undefinedIfEmpty({})).toBeUndefined();
Expand Down
11 changes: 0 additions & 11 deletions libs/util-fn/src/is-empty.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { BadRequestException } from '@nestjs/common';

/**
* Check if an object contains no enumerable properties.
*/
Expand All @@ -12,15 +10,6 @@ export function isEmpty(obj?: object) {
return true;
}

/**
* Throw a BadRequestException (400) of the provided object is empty.
*/
export function throwIfEmpty(obj: object) {
if (isEmpty(obj)) {
throw new BadRequestException();
}
}

/**
* Return undefined if provided object is empty, otherwise return the object.
* Useful for Prisma includes.
Expand Down

0 comments on commit 7c3ea67

Please sign in to comment.