• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GrottoCenter / grottocenter-api / 10996210778

23 Sep 2024 02:17PM UTC coverage: 46.158% (-2.8%) from 48.952%
10996210778

Pull #1306

github

vmarseguerra
feat(entities): adds delete / restore for document
Pull Request #1306: Adds delete / restore for entrance sub entities

740 of 2203 branches covered (33.59%)

Branch coverage included in aggregate %.

526 of 1293 new or added lines in 114 files covered. (40.68%)

23 existing lines in 18 files now uncovered.

2462 of 4734 relevant lines covered (52.01%)

4.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.81
/api/controllers/v1/organization/delete.js
1
const ControllerService = require('../../../services/ControllerService');
1✔
2
const ElasticsearchService = require('../../../services/ElasticsearchService');
1✔
3
const NotificationService = require('../../../services/NotificationService');
1✔
4
const GrottoService = require('../../../services/GrottoService');
1✔
5
const RightService = require('../../../services/RightService');
1✔
6
const { toOrganization } = require('../../../services/mapping/converters');
1✔
7
const NameService = require('../../../services/NameService');
1✔
8

9
module.exports = async (req, res) => {
1✔
NEW
10
  const hasRight = RightService.hasGroup(
×
11
    req.token.groups,
12
    RightService.G.MODERATOR
13
  );
NEW
14
  if (!hasRight) {
×
NEW
15
    return res.forbidden('You are not authorized to delete an organization.');
×
16
  }
17

18
  // Check if organization exists and if it's not already deleted
NEW
19
  const organizationId = req.param('id');
×
20
  const organization =
NEW
21
    await GrottoService.getPopulatedOrganization(organizationId);
×
NEW
22
  if (!organization) {
×
NEW
23
    return res.notFound({
×
24
      message: `Organization of id ${organizationId} not found.`,
25
    });
26
  }
27

NEW
28
  if (!organization.isDeleted) {
×
NEW
29
    const redirectTo = parseInt(req.param('entityId'), 10);
×
NEW
30
    if (!Number.isNaN(redirectTo)) {
×
NEW
31
      organization.redirectTo = redirectTo;
×
NEW
32
      await TGrotto.updateOne({ id: organizationId })
×
33
        .set({ redirectTo })
34
        .catch(() => {});
35
    }
NEW
36
    await TGrotto.destroyOne({ id: organizationId }); // Soft delete
×
NEW
37
    await ElasticsearchService.deleteResource('grottos', organizationId).catch(
×
38
      () => {}
39
    );
NEW
40
    organization.isDeleted = true;
×
41
  }
42

NEW
43
  const deletePermanently = !!req.param('isPermanent');
×
NEW
44
  if (deletePermanently) {
×
NEW
45
    if (
×
46
      organization.exploredNetworks.length > 0 ||
×
47
      organization.exploredEntrances.length > 0 ||
48
      organization.partnerNetworks.length > 0 ||
49
      organization.partnerEntrances.length > 0 ||
50
      organization.cavers.length > 0 ||
51
      organization.documents.length > 0
52
    ) {
53
      // TODO Properly handle the removal of these properties once there are APIs to set/disable them
NEW
54
      return res.status(501).send();
×
55
    }
56

NEW
57
    await TGrotto.update({ redirectTo: organizationId }).set({
×
58
      redirectTo: null,
59
    });
NEW
60
    await TNotification.destroy({ grotto: organizationId });
×
61

NEW
62
    await NameService.permanentDelete({ grotto: organizationId });
×
63

NEW
64
    await HGrotto.destroy({ id: organizationId });
×
NEW
65
    await TGrotto.destroyOne({ id: organizationId }); // Hard delete
×
66
  }
67

NEW
68
  await NotificationService.notifySubscribers(
×
69
    req,
70
    organization,
71
    req.token.id,
72
    deletePermanently
×
73
      ? NotificationService.NOTIFICATION_TYPES.PERMANENT_DELETE
74
      : NotificationService.NOTIFICATION_TYPES.DELETE,
75
    NotificationService.NOTIFICATION_ENTITIES.ORGANIZATION
76
  );
77

NEW
78
  return ControllerService.treatAndConvert(
×
79
    req,
80
    null,
81
    organization,
82
    { controllerMethod: 'GrottoController.delete' },
83
    res,
84
    toOrganization
85
  );
86
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc