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

Admin: Improve deletion of accounts #171

Merged
merged 2 commits into from
Feb 7, 2025
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
11 changes: 10 additions & 1 deletion core/api/admin/admin.models.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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 });
Expand Down
58 changes: 52 additions & 6 deletions test/core/api/admin/admin.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { expect } = require('chai');
const request = require('supertest');
const configTest = require('../../../tasks/config');

Expand All @@ -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);
});
});

Expand Down
Loading