Skip to content

Commit e4902e5

Browse files
authored
Replace remaining direct usages of static Props/SecuredProps (#3422)
This indirection allows them to be provided in different ways, like with a transformer, or external metadata generation. The BaseNodeProps stuff here is clunky. I don't like it, and it could be refined more. But it is not wide spread and maybe we just wait and delete it with Gel migration.
1 parent 9dae7f1 commit e4902e5

File tree

10 files changed

+38
-17
lines changed

10 files changed

+38
-17
lines changed

src/components/authentication/extra-info.resolver.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { ResolveField, Resolver } from '@nestjs/graphql';
22
import { mapValues } from '@seedcompany/common';
3-
import { type AbstractClassType, AnonSession, type Session } from '~/common';
3+
import {
4+
type AbstractClassType,
5+
AnonSession,
6+
EnhancedResource,
7+
type Session,
8+
} from '~/common';
49
import { Privileges } from '../authorization';
510
import { BetaFeatures } from '../authorization/dto/beta-features.dto';
611
import { LoginOutput, RegisterOutput, SessionOutput } from './dto';
@@ -13,7 +18,8 @@ function AuthExtraInfoResolver(concreteClass: AbstractClassType<any>) {
1318
@ResolveField(() => BetaFeatures)
1419
betaFeatures(@AnonSession() session: Session): BetaFeatures {
1520
const privileges = this.privileges.for(session, BetaFeatures);
16-
return mapValues.fromList(BetaFeatures.Props, (prop) =>
21+
const { props } = EnhancedResource.of(BetaFeatures);
22+
return mapValues.fromList([...props], (prop) =>
1723
privileges.can('edit', prop),
1824
).asRecord;
1925
}

src/components/file/media/media.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const resolveMedia = (val: Pick<AnyMedia, '__typename'>) => {
4141
@DbLabel(null)
4242
export class MediaUserMetadata extends DataObject {
4343
static readonly Props: string[] = keysOf<MediaUserMetadata>();
44+
static readonly SecuredProps: string[] = [];
4445

4546
@NameField({
4647
description: stripIndent`

src/components/file/media/media.repository.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ export class MediaRepository extends CommonRepository {
8484
const res = input.__typename
8585
? EnhancedResource.of(resolveMedia(input as AnyMedia))
8686
: undefined;
87+
const metadata = EnhancedResource.of(MediaUserMetadata);
88+
8789
const tempId = await generateId();
8890
const query = this.db
8991
.query()
@@ -147,7 +149,7 @@ export class MediaRepository extends CommonRepository {
147149
node: String(
148150
apoc.map.submap(
149151
apoc.map.merge('node', 'prevMedia'),
150-
MediaUserMetadata.Props.map((k) => `'${k}'`),
152+
[...metadata.props].map((k) => `'${k}'`),
151153
[],
152154
false,
153155
),

src/components/notifications/notification.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class NotificationRepository extends CommonRepository {
5151
input: Record<string, any>,
5252
session: Session,
5353
) {
54-
const extra = omit(input, Notification.Props);
54+
const extra = omit(input, [...EnhancedResource.of(Notification).props]);
5555
const res = await this.db
5656
.query()
5757
.create(

src/components/progress-report/media/dto/media.dto.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Field, InputType, ObjectType } from '@nestjs/graphql';
22
import { setOf } from '@seedcompany/common';
33
import { keys as keysOf } from 'ts-transformer-keys';
44
import {
5+
EnhancedResource,
56
IdField,
67
type IdOf,
78
Resource,
@@ -27,7 +28,12 @@ export type VariantGroup = IdOf<'ProgressReportMediaVariantGroup'>;
2728
export class ProgressReportMedia extends Resource {
2829
static Props = keysOf<ProgressReportMedia>();
2930
static SecuredProps = keysOf<SecuredProps<ProgressReportMedia>>();
30-
static BaseNodeProps = [...Resource.Props, 'category', 'creator', 'variant'];
31+
static BaseNodeProps = [
32+
...EnhancedResource.of(Resource).props,
33+
'category',
34+
'creator',
35+
'variant',
36+
];
3137
static readonly Parent = () =>
3238
import('../../dto/progress-report.dto').then((m) => m.ProgressReport);
3339
static readonly ConfirmThisClassPassesSensitivityToPolicies = true;

src/components/project/dto/project.dto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
DbSort,
1313
DbUnique,
1414
Disabled,
15+
EnhancedResource,
1516
IntersectTypes,
1617
NameField,
1718
parentIdMiddleware,
@@ -86,7 +87,10 @@ const RequiredWhenNotInDev = RequiredWhen(() => Project)({
8687
class Project extends Interfaces {
8788
static readonly Props: string[] = keysOf<Project>();
8889
static readonly SecuredProps: string[] = keysOf<SecuredProps<Project>>();
89-
static readonly BaseNodeProps = [...Resource.Props, 'type'];
90+
static readonly BaseNodeProps = [
91+
...EnhancedResource.of(Resource).props,
92+
'type',
93+
];
9094
static readonly Relations = () =>
9195
({
9296
rootDirectory: Directory,

src/core/database/changes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ export const getChanges =
129129
Record<Exclude<keyof Changes, keyof AnyChangesOf<TResource>>, never>,
130130
): ChangesOf<TResource, Changes> => {
131131
const res = EnhancedResource.of(resource);
132-
const actual = pickBy(omit(changes, Resource.Props), (change, prop) => {
132+
const baseImmutable = [...EnhancedResource.of(Resource).props];
133+
const actual = pickBy(omit(changes, baseImmutable), (change, prop) => {
133134
if (change === undefined) {
134135
return false;
135136
}
@@ -159,7 +160,7 @@ export const getChanges =
159160

160161
if (
161162
Object.keys(actual).length > 0 &&
162-
resource.Props.includes('modifiedAt') &&
163+
res.props.has('modifiedAt') &&
163164
!(actual as any).modifiedAt
164165
) {
165166
return {

src/core/database/common.repository.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,10 @@ export class CommonRepository {
169169
}
170170

171171
protected getConstraintsFor(resource: ResourceShape<any>) {
172-
const { dbLabel } = EnhancedResource.of(resource);
172+
const { dbLabel, props } = EnhancedResource.of(resource);
173173
return [
174-
...(resource.Props.includes('id')
175-
? [createUniqueConstraint(dbLabel, 'id')]
176-
: []),
177-
...resource.Props.flatMap((prop) => {
174+
...(props.has('id') ? [createUniqueConstraint(dbLabel, 'id')] : []),
175+
...[...props].flatMap((prop) => {
178176
const label = DbUnique.get(resource, prop);
179177
return label
180178
? createUniqueConstraint(label, 'value', `${resource.name}_${prop}`)

src/core/database/dto.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export const DtoRepository = <
6565
return !exists;
6666
}
6767
@Once() private get uniqueLabel() {
68-
const labels = resource.Props.flatMap(
69-
(p) => DbUnique.get(resource, p) ?? [],
68+
const labels = [...this.resource.props].flatMap(
69+
(p: string) => DbUnique.get(resource, p) ?? [],
7070
);
7171
if (labels.length === 0) {
7272
return new ServerException(

src/core/database/query/sorting.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { node, type Query, relation } from 'cypher-query-builder';
33
import { identity } from 'rxjs';
44
import { type LiteralUnion } from 'type-fest';
55
import {
6+
EnhancedResource,
67
type MadeEnum,
78
type Order,
89
Resource,
@@ -146,8 +147,10 @@ export const defineSorters = <TResourceStatic extends ResourceShape<any>>(
146147
return { ...common, matcher: subCustom, sort: subField };
147148
}
148149

149-
const baseNodeProps = resource.BaseNodeProps ?? Resource.Props;
150-
const isBaseNodeProp = baseNodeProps.includes(sort);
150+
const baseNodeProps = new Set(
151+
resource.BaseNodeProps ?? EnhancedResource.of(Resource).props,
152+
);
153+
const isBaseNodeProp = baseNodeProps.has(sort);
151154
const matcher = (isBaseNodeProp ? basePropSorter : propSorter)(sort);
152155
return { ...common, matcher };
153156
};

0 commit comments

Comments
 (0)