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: allow cascadingDeleteCause to be passed in via public deletion mutator methods #248

Closed
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
4 changes: 2 additions & 2 deletions packages/entity/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default abstract class Entity<
.getViewerScopedEntityCompanionForClass(this)
.getMutatorFactory()
.forDelete(existingEntity, queryContext)
.deleteAsync();
.deleteAsync(null);
}

/**
Expand Down Expand Up @@ -195,7 +195,7 @@ export default abstract class Entity<
.getViewerScopedEntityCompanionForClass(this)
.getMutatorFactory()
.forDelete(existingEntity, queryContext)
.enforceDeleteAsync();
.enforceDeleteAsync(null);
}
}

Expand Down
12 changes: 8 additions & 4 deletions packages/entity/src/EntityMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,19 +599,23 @@ export class DeleteMutator<
* Delete the entity after authorizing against delete privacy rules. The entity is invalidated in all caches.
* @returns void result, where result error can be UnauthorizedError
*/
async deleteAsync(): Promise<Result<void>> {
async deleteAsync(
cascadingDeleteCause: EntityCascadingDeletionInfo | null,
): Promise<Result<void>> {
return await timeAndLogMutationEventAsync(
this.metricsAdapter,
EntityMetricsMutationType.DELETE,
this.entityClass.name,
)(this.deleteInTransactionAsync(new Set(), false, null));
)(this.deleteInTransactionAsync(new Set(), false, cascadingDeleteCause));
}

/**
* Convenience method that throws upon delete failure.
*/
async enforceDeleteAsync(): Promise<void> {
return await enforceAsyncResult(this.deleteAsync());
async enforceDeleteAsync(
cascadingDeleteCause: EntityCascadingDeletionInfo | null,
): Promise<void> {
return await enforceAsyncResult(this.deleteAsync(cascadingDeleteCause));
}

private async deleteInTransactionAsync(
Expand Down
16 changes: 9 additions & 7 deletions packages/entity/src/__tests__/EntityMutator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ describe(EntityMutatorFactory, () => {
);
expect(existingEntity).toBeTruthy();

await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync();
await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync(null);

await expect(
enforceAsyncResult(
Expand Down Expand Up @@ -921,7 +921,7 @@ describe(EntityMutatorFactory, () => {
.loadByIDAsync(id1),
);

await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync();
await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync(null);

verify(
spiedPrivacyPolicy.authorizeDeleteAsync(
Expand Down Expand Up @@ -972,7 +972,7 @@ describe(EntityMutatorFactory, () => {
.loadByIDAsync(id1),
);

await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync();
await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync(null);

verifyTriggerCounts(
viewerContext,
Expand Down Expand Up @@ -1027,7 +1027,7 @@ describe(EntityMutatorFactory, () => {
.loadByIDAsync(id1),
);

await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync();
await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync(null);

verifyValidatorCounts(viewerContext, validatorSpies, 0, {
type: EntityMutationType.DELETE as any,
Expand Down Expand Up @@ -1246,7 +1246,7 @@ describe(EntityMutatorFactory, () => {

const entityDeleteResult = await entityMutatorFactory
.forDelete(fakeEntity, queryContext)
.deleteAsync();
.deleteAsync(null);
expect(entityDeleteResult.ok).toBe(false);
expect(entityDeleteResult.reason).toEqual(rejectionError);
expect(entityDeleteResult.value).toBe(undefined);
Expand Down Expand Up @@ -1364,7 +1364,7 @@ describe(EntityMutatorFactory, () => {
entityMutatorFactory.forUpdate(fakeEntity, queryContext).updateAsync(),
).rejects.toEqual(rejectionError);
await expect(
entityMutatorFactory.forDelete(fakeEntity, queryContext).deleteAsync(),
entityMutatorFactory.forDelete(fakeEntity, queryContext).deleteAsync(null),
).rejects.toEqual(rejectionError);
});

Expand All @@ -1388,7 +1388,9 @@ describe(EntityMutatorFactory, () => {
.updateAsync(),
);

await enforceAsyncResult(entityMutatorFactory.forDelete(newEntity, queryContext).deleteAsync());
await enforceAsyncResult(
entityMutatorFactory.forDelete(newEntity, queryContext).deleteAsync(null),
);

verify(
spiedMetricsAdapter.logMutatorMutationEvent(
Expand Down
Loading