Skip to content

Commit a73d9ea

Browse files
committed
Refactor FieldZone service to move neo4j needs into neo4j repo
1 parent f531e47 commit a73d9ea

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

src/components/field-zone/field-zone.repository.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { Injectable } from '@nestjs/common';
22
import { node, Query, relation } from 'cypher-query-builder';
33
import { DateTime } from 'luxon';
44
import { ChangesOf } from '~/core/database/changes';
5-
import { ID, Session, UnsecuredDto } from '../../common';
5+
import {
6+
DuplicateException,
7+
ID,
8+
SecuredList,
9+
ServerException,
10+
Session,
11+
UnsecuredDto,
12+
} from '../../common';
613
import { DtoRepository } from '../../core';
714
import {
815
ACTIVE,
@@ -25,6 +32,13 @@ import {
2532
@Injectable()
2633
export class FieldZoneRepository extends DtoRepository(FieldZone) {
2734
async create(input: CreateFieldZone, session: Session) {
35+
if (!(await this.isUnique(input.name))) {
36+
throw new DuplicateException(
37+
'fieldZone.name',
38+
'FieldZone with this name already exists.',
39+
);
40+
}
41+
2842
const initialProps = {
2943
name: input.name,
3044
canDelete: true,
@@ -42,7 +56,12 @@ export class FieldZoneRepository extends DtoRepository(FieldZone) {
4256
)
4357
.return<{ id: ID }>('node.id as id');
4458

45-
return await query.first();
59+
const result = await query.first();
60+
if (!result) {
61+
throw new ServerException('failed to create field zone');
62+
}
63+
64+
return await this.readOne(result.id);
4665
}
4766

4867
protected hydrate() {
@@ -62,7 +81,7 @@ export class FieldZoneRepository extends DtoRepository(FieldZone) {
6281
}
6382

6483
async update(
65-
existing: FieldZone,
84+
existing: Pick<FieldZone, 'id'>,
6685
changes: ChangesOf<FieldZone, UpdateFieldZone>,
6786
) {
6887
const { directorId, ...simpleChanges } = changes;
@@ -72,6 +91,8 @@ export class FieldZoneRepository extends DtoRepository(FieldZone) {
7291
}
7392

7493
await this.updateProperties(existing, simpleChanges);
94+
95+
return await this.readOne(existing.id);
7596
}
7697

7798
private async updateDirector(directorId: ID, id: ID) {
@@ -103,6 +124,9 @@ export class FieldZoneRepository extends DtoRepository(FieldZone) {
103124
}
104125

105126
async list({ filter, ...input }: FieldZoneListInput, session: Session) {
127+
if (!this.privileges.forUser(session).can('read')) {
128+
return SecuredList.Redacted;
129+
}
106130
const result = await this.db
107131
.query()
108132
.match(requestingUser(session))
Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { Injectable } from '@nestjs/common';
22
import {
3-
DuplicateException,
43
ID,
54
ObjectView,
6-
SecuredList,
75
ServerException,
86
Session,
97
UnsecuredDto,
108
} from '../../common';
119
import { HandleIdLookup, ILogger, Logger } from '../../core';
12-
import { mapListResults } from '../../core/database/results';
1310
import { Privileges } from '../authorization';
1411
import {
1512
CreateFieldZone,
@@ -30,20 +27,8 @@ export class FieldZoneService {
3027

3128
async create(input: CreateFieldZone, session: Session): Promise<FieldZone> {
3229
this.privileges.for(session, FieldZone).verifyCan('create');
33-
if (!(await this.repo.isUnique(input.name))) {
34-
throw new DuplicateException(
35-
'fieldZone.name',
36-
'FieldZone with this name already exists.',
37-
);
38-
}
39-
const result = await this.repo.create(input, session);
40-
41-
if (!result) {
42-
throw new ServerException('failed to create field zone');
43-
}
44-
45-
this.logger.debug(`field zone created`, { id: result.id });
46-
return await this.readOne(result.id, session);
30+
const dto = await this.repo.create(input, session);
31+
return this.secure(dto, session);
4732
}
4833

4934
@HandleIdLookup(FieldZone)
@@ -58,32 +43,26 @@ export class FieldZoneService {
5843
});
5944

6045
const result = await this.repo.readOne(id);
61-
return await this.secure(result, session);
46+
return this.secure(result, session);
6247
}
6348

6449
async readMany(ids: readonly ID[], session: Session) {
6550
const fieldZones = await this.repo.readMany(ids);
66-
return await Promise.all(
67-
fieldZones.map((dto) => this.secure(dto, session)),
68-
);
51+
return fieldZones.map((dto) => this.secure(dto, session));
6952
}
7053

71-
private async secure(
72-
dto: UnsecuredDto<FieldZone>,
73-
session: Session,
74-
): Promise<FieldZone> {
54+
private secure(dto: UnsecuredDto<FieldZone>, session: Session) {
7555
return this.privileges.for(session, FieldZone).secure(dto);
7656
}
7757

7858
async update(input: UpdateFieldZone, session: Session): Promise<FieldZone> {
79-
const fieldZone = await this.readOne(input.id, session);
59+
const fieldZone = await this.repo.readOne(input.id);
8060

8161
const changes = this.repo.getActualChanges(fieldZone, input);
8262
this.privileges.for(session, FieldZone, fieldZone).verifyChanges(changes);
8363

84-
await this.repo.update(fieldZone, changes);
85-
86-
return await this.readOne(input.id, session);
64+
const updated = await this.repo.update(fieldZone, changes);
65+
return this.secure(updated, session);
8766
}
8867

8968
async delete(id: ID, session: Session): Promise<void> {
@@ -103,11 +82,10 @@ export class FieldZoneService {
10382
input: FieldZoneListInput,
10483
session: Session,
10584
): Promise<FieldZoneListOutput> {
106-
if (this.privileges.for(session, FieldZone).can('read')) {
107-
const results = await this.repo.list(input, session);
108-
return await mapListResults(results, (dto) => this.secure(dto, session));
109-
} else {
110-
return SecuredList.Redacted;
111-
}
85+
const results = await this.repo.list(input, session);
86+
return {
87+
...results,
88+
items: results.items.map((dto) => this.secure(dto, session)),
89+
};
11290
}
11391
}

0 commit comments

Comments
 (0)