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

feat: throw error if duplicate map #1082

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/backend-e2e/src/maps.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,30 @@ describe('Maps', () => {
});
});

it('should throw 409 Conflict if map with same BSP hash is submitted again', async () => {
await uploadBspToPreSignedUrl(bspBuffer, token);

await req.postAttach({
url: 'maps',
status: 201,
data: createMapObject,
token
});

await uploadBspToPreSignedUrl(bspBuffer, token);

const res = await req.postAttach({
url: 'maps',
status: 409,
data: createMapObject,
token
});

expect(res.body.message).toBe(
'Map with this file hash already exists'
);
});

it('should succeed if VMF file is missing', async () => {
await uploadBspToPreSignedUrl(bspBuffer, token);

Expand Down Expand Up @@ -2325,6 +2349,26 @@ describe('Maps', () => {
});
});

it('should throw 409 Conflict if map with same BSP hash is submitted again', async () => {
await uploadBspToPreSignedUrl(bspBuffer, u1Token);

await req.postAttach({
url: `maps/${map.id}`,
status: 201,
data: {},
token: u1Token
});

const res = await req.postAttach({
url: `maps/${map.id}`,
status: 409,
data: {},
token: u1Token
});

expect(res.body.message).toBe('Map with this file hash already exists');
});

for (const status of [MapStatus.APPROVED, MapStatus.DISABLED]) {
it(`should 403 if the map status is ${MapStatus[status]}`, async () => {
await prisma.mMap.update({ where: { id: map.id }, data: { status } });
Expand Down
18 changes: 18 additions & 0 deletions apps/backend/src/app/modules/maps/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,15 @@ export class MapsService {
const hasVmf = vmfFiles?.length > 0;
const bspHash = FileStoreService.getHashForBuffer(bspFile.buffer);

// Check for duplicate map hash
const existingMap = await this.db.mMap.exists({
where: { versions: { some: { bspHash } } }
});

if (existingMap) {
throw new ConflictException('Map with this file hash already exists');
}

let map: Awaited<ReturnType<typeof this.createMapDbEntry>>;

const tasks: Promise<any>[] = [
Expand Down Expand Up @@ -661,6 +670,15 @@ export class MapsService {
const hasVmf = vmfFiles?.length > 0;
const bspHash = FileStoreService.getHashForBuffer(bspFile.buffer);

// Check for duplicate map hash
const existingMap = await this.db.mMap.exists({
where: { versions: { some: { bspHash } } }
});

if (existingMap) {
throw new ConflictException('Map with this file hash already exists');
}

let zonesStr: string;
let zones: MapZones;
if (dto.zones) {
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
postgres:
environment: &env
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
postgres:
container_name: postgres
Expand Down