Skip to content

Commit

Permalink
Add removeByDomain method to domains.records.service.js
Browse files Browse the repository at this point in the history
  • Loading branch information
FLYBYME committed Feb 4, 2024
1 parent bc01945 commit fd96cb3
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions services/domains.records.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,37 @@ module.exports = {
});
}
},

/**
* remove records by domain
*
* @actions
* @param {String} domain - domain id
*
* @returns {Number} - number of removed records
*/
removeByDomain: {
params: {
domain: { type: "string" }
},
async handler(ctx) {
const domain = ctx.params.domain;
const entities = await this.findEntities(null, {
query: { domain },
fields: ["id"],
scope: false
});
await this.Promise.all(
entities.map(entity =>
this.removeEntity(ctx, { id: entity.id, scope: false })
.catch((err) => {
this.logger.error(`Error removing domain record ${entity.id}`, err)
})
)
);
return entities.length;
}
},
},

/**
Expand All @@ -266,21 +297,9 @@ module.exports = {
async "domains.removed"(ctx) {
const domain = ctx.params.data;

const entities = await this.findEntities(null, {
query: { domain: domain.id },
fields: ["id"],
scope: false
});
await this.Promise.all(
entities.map(entity =>
this.removeEntity(ctx, { id: entity.id, scope: false })
.catch((err) => {
this.logger.error(`Error removing domain record ${entity.id}`, err)
})
)
);

this.logger.info(`Removed ${entities.length} records for domain ${domain.domain}`)
const entities = await this.actions.removeByDomain({ domain: domain.id });

this.logger.info(`Removed ${entities} records for domain ${domain.domain}`)

},
},
Expand Down

0 comments on commit fd96cb3

Please sign in to comment.