Skip to content

Commit 5f1a237

Browse files
committed
Allow updates to objects that have missing fields if the update reduces the missing number
This allows us to fill these missing data points on our bad data. Hopefully this is temporary.
1 parent c3a7ec0 commit 5f1a237

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

src/common/required-when.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,37 @@ export const RequiredWhen =
4141
...fieldOptions,
4242
});
4343

44-
RequiredWhen.verify = <TResourceStatic extends ResourceShape<any>>(
44+
RequiredWhen.calc = <TResourceStatic extends ResourceShape<any>>(
4545
resource: TResourceStatic,
4646
obj: UnsecuredDto<TResourceStatic['prototype']>,
4747
) => {
4848
const res = EnhancedResource.of(resource);
49-
const missing = [...res.props].flatMap((prop: string) => {
49+
const conditions = [...res.props].flatMap((prop: string) => {
5050
const condition = RequiredWhenMetadata.get(resource, prop);
51-
return condition?.isEnabled(obj) &&
52-
(condition.isMissing?.(obj) ?? obj[prop] == null)
51+
return condition ? { ...condition, field: prop } : [];
52+
});
53+
const missing = conditions.flatMap((condition) => {
54+
return condition.isEnabled(obj) &&
55+
(condition.isMissing?.(obj) ?? obj[condition.field] == null)
5356
? {
54-
field: condition.field ?? prop,
57+
field: condition.field,
5558
description: condition.description,
5659
}
5760
: [];
5861
});
5962
if (missing.length > 0) {
60-
throw new MissingRequiredFieldsException(res, { id: obj.id }, missing);
63+
return new MissingRequiredFieldsException(res, { id: obj.id }, missing);
64+
}
65+
return undefined;
66+
};
67+
68+
RequiredWhen.verify = <TResourceStatic extends ResourceShape<any>>(
69+
resource: TResourceStatic,
70+
obj: UnsecuredDto<TResourceStatic['prototype']>,
71+
) => {
72+
const ex = RequiredWhen.calc(resource, obj);
73+
if (ex) {
74+
throw ex;
6175
}
6276
};
6377

src/components/engagement/engagement.service.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,14 @@ export class EngagementService {
196196
changeset,
197197
);
198198

199-
RequiredWhen.verify(LanguageEngagement, updated);
199+
const prevMissing = RequiredWhen.calc(LanguageEngagement, previous);
200+
const nowMissing = RequiredWhen.calc(LanguageEngagement, updated);
201+
if (
202+
nowMissing &&
203+
(!prevMissing || nowMissing.missing.length >= prevMissing.missing.length)
204+
) {
205+
throw nowMissing;
206+
}
200207

201208
const event = new EngagementUpdatedEvent(
202209
updated,
@@ -245,7 +252,14 @@ export class EngagementService {
245252
changeset,
246253
);
247254

248-
RequiredWhen.verify(InternshipEngagement, updated);
255+
const prevMissing = RequiredWhen.calc(InternshipEngagement, previous);
256+
const nowMissing = RequiredWhen.calc(InternshipEngagement, updated);
257+
if (
258+
nowMissing &&
259+
(!prevMissing || nowMissing.missing.length >= prevMissing.missing.length)
260+
) {
261+
throw nowMissing;
262+
}
249263

250264
const event = new EngagementUpdatedEvent(
251265
updated,

src/components/project/project.service.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,14 @@ export class ProjectService {
343343

344344
updated = await this.repo.update(updated, changes, changeset);
345345

346-
RequiredWhen.verify(IProject, updated);
346+
const prevMissing = RequiredWhen.calc(IProject, currentProject);
347+
const nowMissing = RequiredWhen.calc(IProject, updated);
348+
if (
349+
nowMissing &&
350+
(!prevMissing || nowMissing.missing.length >= prevMissing.missing.length)
351+
) {
352+
throw nowMissing;
353+
}
347354

348355
const event = new ProjectUpdatedEvent(
349356
updated,

0 commit comments

Comments
 (0)