From fd96cb36462ceed970b03960774df59c0ed6e24a Mon Sep 17 00:00:00 2001 From: FLYBYME Date: Sat, 3 Feb 2024 21:33:07 -0500 Subject: [PATCH] Add removeByDomain method to domains.records.service.js --- services/domains.records.service.js | 49 ++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/services/domains.records.service.js b/services/domains.records.service.js index af04317..d5f8c5f 100644 --- a/services/domains.records.service.js +++ b/services/domains.records.service.js @@ -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; + } + }, }, /** @@ -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}`) }, },