Skip to content

Limit DtoRepo.deleteNode to filter to the resource type by default #3222

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

Merged
merged 3 commits into from
May 30, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/components/budget/budget.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class BudgetService {

async deleteRecord(id: ID, session: Session, changeset?: ID): Promise<void> {
try {
await this.budgetRecordsRepo.deleteNode(id, changeset);
await this.budgetRecordsRepo.deleteNode(id, { changeset });
} catch (e) {
this.logger.warning('Failed to delete Budget Record', {
exception: e,
Expand Down
2 changes: 1 addition & 1 deletion src/components/engagement/engagement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export class EngagementService {
await this.eventBus.publish(new EngagementWillDeleteEvent(object, session));

try {
await this.repo.deleteNode(object, changeset);
await this.repo.deleteNode(object, { changeset });
} catch (e) {
this.logger.warning('Failed to delete Engagement', {
exception: e,
Expand Down
2 changes: 1 addition & 1 deletion src/components/partnership/partnership.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class PartnershipService {
);

try {
await this.repo.deleteNode(object, changeset);
await this.repo.deleteNode(object, { changeset });
} catch (exception) {
this.logger.error('Failed to delete', { id, exception });
throw new ServerException('Failed to delete', exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ACTIVE,
createNode,
createRelationships,
deleteBaseNode,
filter,
matchProjectScopedRoles,
matchProjectSens,
Expand Down Expand Up @@ -233,17 +234,14 @@ export class ProgressReportMediaRepository extends DtoRepository<
.run();
}

async isVariantGroupEmpty(id: string) {
const hasVariant = await this.db
async deleteVariantGroupIfEmpty(id: string) {
await this.db
.query()
.match([
node('variantGroup', 'VariantGroup', { id }),
relation('out', '', 'child', ACTIVE),
node('variant', 'ProgressReportMedia'),
])
.return('variant')
.first();
return !hasVariant;
.match(node('variantGroup', 'VariantGroup', { id }))
.raw('where not exists((variantGroup)-[:child { active: true }]->())')
.apply(deleteBaseNode('variantGroup'))
.return('*')
.executeAndLogStats();
}

protected hydrate(session: Session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ export class ProgressReportMediaService {
.verifyCan('delete');

await this.repo.deleteNode(id);
if (await this.repo.isVariantGroupEmpty(media.variantGroup)) {
await this.repo.deleteNode(media.variantGroup);
}
await this.repo.deleteVariantGroupIfEmpty(media.variantGroup);

return media.report;
}
Expand Down
28 changes: 20 additions & 8 deletions src/core/database/common.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ import {
ResourceShape,
ServerException,
} from '~/common';
import { ResourceLike, ResourcesHost } from '../resources';
import { DatabaseService } from './database.service';
import { createUniqueConstraint } from './indexer';
import { ACTIVE, updateRelationList } from './query';
import { ACTIVE, deleteBaseNode, updateRelationList } from './query';
import { BaseNode } from './results';

/**
* This provides a few methods out of the box.
*/
@Injectable()
export class CommonRepository {
@Inject(DatabaseService)
protected db: DatabaseService;
@Inject() protected db: DatabaseService;
@Inject() protected readonly resources: ResourcesHost;

async getBaseNode(
id: ID,
Expand Down Expand Up @@ -130,17 +131,28 @@ export class CommonRepository {
return res.stats;
}

async deleteNode(objectOrId: { id: ID } | ID, changeset?: ID) {
async deleteNode(
objectOrId: { id: ID } | ID,
{ changeset, resource }: { changeset?: ID; resource?: ResourceLike } = {},
) {
const id = isIdLike(objectOrId) ? objectOrId : objectOrId.id;
const label = resource
? this.resources.enhance(resource).dbLabel
: 'BaseNode';
if (!changeset) {
await this.db.deleteNode(objectOrId);
await this.db
.query()
.matchNode('node', label, { id })
.apply(deleteBaseNode('node'))
.return('*')
.run();
return;
}
try {
const id = isIdLike(objectOrId) ? objectOrId : objectOrId.id;
await this.db
.query()
.match([node('node', 'BaseNode', { id })])
.match([node('changeset', 'Changeset', { id: changeset })])
.match(node('node', label, { id }))
.match(node('changeset', 'Changeset', { id: changeset }))
.merge([
node('changeset'),
relation('out', 'rel', 'changeset'),
Expand Down
11 changes: 11 additions & 0 deletions src/core/database/dto.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
UnsecuredDto,
} from '~/common';
import { Privileges } from '../../components/authorization';
import { ResourceLike } from '../resources';
import { DbChanges, getChanges } from './changes';
import { CommonRepository } from './common.repository';
import { DbTypeOf } from './db-type';
Expand Down Expand Up @@ -104,6 +105,16 @@ export const DtoRepository = <
.run();
}

async deleteNode(
objectOrId: { id: ID } | ID,
options: { changeset?: ID; resource?: ResourceLike } = {},
) {
await super.deleteNode(objectOrId, {
resource: this.resource,
...options,
});
}

protected async updateProperties<
TObject extends Partial<TResource | UnsecuredDto<TResource>> & {
id: ID;
Expand Down
8 changes: 6 additions & 2 deletions src/core/edgedb/common.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ export class CommonRepository implements PublicOf<Neo4jCommonRepository> {
);
}

async deleteNode(objectOrId: { id: ID } | ID, _changeset?: ID) {
async deleteNode(
objectOrId: { id: ID } | ID,
{ resource }: { changeset?: ID; resource?: ResourceLike } = {},
) {
const id = isIdLike(objectOrId) ? objectOrId : objectOrId.id;
const query = e.delete(e.Object, () => ({
const type = resource ? this.resources.enhance(resource).db : e.Object;
const query = e.delete(type, () => ({
filter_single: { id },
}));
await this.db.run(query);
Expand Down
10 changes: 10 additions & 0 deletions src/core/edgedb/dto.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ export const RepoFor = <
async getBaseNodes(ids: readonly ID[], fqn?: ResourceLike) {
return await super.getBaseNodes(ids, fqn ?? resource);
}

async deleteNode(
objectOrId: { id: ID } | ID,
options: { changeset?: ID; resource?: ResourceLike } = {},
) {
await super.deleteNode(objectOrId, {
resource: this.resource,
...options,
});
}
}

const readManyQuery = e.params({ ids: e.array(e.uuid) }, ({ ids }) => {
Expand Down
Loading