From eed348f68942bcd6690e163084b530e7db06b2c3 Mon Sep 17 00:00:00 2001 From: Pierre-Gilles Leymarie Date: Fri, 7 Feb 2025 10:58:01 +0100 Subject: [PATCH] Admin: Improve deletion of accounts (#171) --- core/api/admin/admin.models.js | 11 +++- test/core/api/admin/admin.controller.test.js | 58 ++++++++++++++++++-- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/core/api/admin/admin.models.js b/core/api/admin/admin.models.js index d6eabc80..4ca82b35 100644 --- a/core/api/admin/admin.models.js +++ b/core/api/admin/admin.models.js @@ -73,8 +73,8 @@ module.exports = function AdminModel(logger, db, redisClient, mailService, slack await Promise.map( backups, async (backup) => { - const key = path.basename(backup.path); try { + const key = path.basename(backup.path); await s3.deleteObject({ Bucket: process.env.STORAGE_BUCKET, Key: key }).promise(); } catch (e) { logger.warn(`Fail to delete ${backup.path}`); @@ -92,6 +92,15 @@ module.exports = function AdminModel(logger, db, redisClient, mailService, slack await db.t_reset_password.destroy({ user_id: user.id }); await db.t_user.destroy({ id: user.id }); }); + // Getting enedis data + const usagePoints = await db.t_enedis_usage_point.find({ account_id: accountId }); + await Promise.mapSeries(usagePoints, async (usagePoint) => { + await db.t_enedis_daily_consumption.destroy({ usage_point_id: usagePoint.usage_point_id }); + await db.t_enedis_consumption_load_curve.destroy({ usage_point_id: usagePoint.usage_point_id }); + await db.t_enedis_sync.destroy({ usage_point_id: usagePoint.usage_point_id }); + await db.t_enedis_usage_point.destroy({ usage_point_id: usagePoint.usage_point_id }); + }); + // Delete rest await db.t_backup.destroy({ account_id: accountId }); await db.t_account_payment_activity.destroy({ account_id: accountId }); await db.t_instance.destroy({ account_id: accountId }); diff --git a/test/core/api/admin/admin.controller.test.js b/test/core/api/admin/admin.controller.test.js index 0fc7f842..411091fe 100644 --- a/test/core/api/admin/admin.controller.test.js +++ b/test/core/api/admin/admin.controller.test.js @@ -1,3 +1,4 @@ +const { expect } = require('chai'); const request = require('supertest'); const configTest = require('../../../tasks/config'); @@ -20,18 +21,63 @@ describe('POST /admin/accounts/:id/resend', () => { }); describe('DELETE /admin/accounts/:id', () => { - it('should delete account', function Test() { + it('should delete account', async function Test() { this.timeout(5000); process.env.SUPER_ADMIN_USER_ID = 'a139e4a6-ec6c-442d-9730-0499155d38d4'; - return request(TEST_BACKEND_APP) + await TEST_DATABASE_INSTANCE.t_enedis_usage_point.insert({ + usage_point_id: '1111111111', + account_id: 'be2b9666-5c72-451e-98f4-efca76ffef54', + created_at: '2023-12-29 05:29:50.908699+00', + }); + await TEST_DATABASE_INSTANCE.t_enedis_sync.insert({ + usage_point_id: '1111111111', + jobs_done: 0, + jobs_total: 2, + }); + await TEST_DATABASE_INSTANCE.t_enedis_daily_consumption.insert({ + usage_point_id: '1111111111', + value: 1, + created_at: '2025-02-06', + }); + await TEST_DATABASE_INSTANCE.t_enedis_consumption_load_curve.insert({ + usage_point_id: '1111111111', + value: 1, + created_at: '2022-11-28 18:00:00+00', + }); + // This should not be purged + await TEST_DATABASE_INSTANCE.t_enedis_usage_point.insert({ + usage_point_id: '222222222222', + account_id: 'b2d23f66-487d-493f-8acb-9c8adb400def', + created_at: '2023-12-29 05:29:50.908699+00', + }); + await TEST_DATABASE_INSTANCE.t_enedis_daily_consumption.insert({ + usage_point_id: '222222222222', + value: 1, + created_at: '2025-02-06', + }); + await TEST_DATABASE_INSTANCE.t_enedis_consumption_load_curve.insert({ + usage_point_id: '222222222222', + value: 1, + created_at: '2022-11-28 18:00:00+00', + }); + await request(TEST_BACKEND_APP) .delete('/admin/accounts/be2b9666-5c72-451e-98f4-efca76ffef54') .set('Accept', 'application/json') .set('Authorization', configTest.jwtAccessTokenDashboard) .expect('Content-Type', /json/) - .expect(200) - .then((response) => { - response.body.should.have.property('status', 200); - }); + .expect(200); + const usagePoints = await TEST_DATABASE_INSTANCE.t_enedis_usage_point.find({ + usage_point_id: '222222222222', + }); + expect(usagePoints).to.have.lengthOf(1); + const consumptions = await TEST_DATABASE_INSTANCE.t_enedis_daily_consumption.find({ + usage_point_id: '222222222222', + }); + expect(consumptions).to.have.lengthOf(1); + const consumptionLoadCurves = await TEST_DATABASE_INSTANCE.t_enedis_consumption_load_curve.find({ + usage_point_id: '222222222222', + }); + expect(consumptionLoadCurves).to.have.lengthOf(1); }); });