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

hicommonwealth / commonwealth / 13497108514

24 Feb 2025 11:36AM UTC coverage: 46.449% (+0.08%) from 46.365%
13497108514

Pull #11078

github

web-flow
Merge 56f84aad3 into beadf67b7
Pull Request #11078: Improvements to Community Homepages

1317 of 3113 branches covered (42.31%)

Branch coverage included in aggregate %.

2490 of 5083 relevant lines covered (48.99%)

38.03 hits per line

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

82.76
/libs/model/src/quest/UpdateQuest.command.ts
1
import { Command } from '@hicommonwealth/core';
2
import * as schemas from '@hicommonwealth/schemas';
3
import { models } from '../database';
4
import { isSuperAdmin } from '../middleware';
5
import {
6
  mustBeValidDateRange,
7
  mustExist,
8
  mustNotBeStarted,
9
  mustNotExist,
10
} from '../middleware/guards';
11
import { getDelta } from '../utils';
12

13
export function UpdateQuest(): Command<typeof schemas.UpdateQuest> {
14
  return {
8✔
15
    ...schemas.UpdateQuest,
16
    auth: [isSuperAdmin],
17
    secure: true,
18
    body: async ({ payload }) => {
19
      const {
20
        quest_id,
21
        name,
22
        description,
23
        community_id,
24
        image_url,
25
        start_date,
26
        end_date,
27
        action_metas,
28
      } = payload;
8✔
29

30
      const quest = await models.Quest.findOne({ where: { id: quest_id } });
8✔
31
      mustExist(`Quest with id "${quest_id}`, quest);
8✔
32

33
      if (name) {
8✔
34
        const existingName = await models.Quest.findOne({
1✔
35
          where: { community_id: community_id ?? null, name },
1!
36
          attributes: ['id'],
37
        });
38
        mustNotExist(
1✔
39
          `Quest named "${name}" in community "${community_id}"`,
40
          existingName,
41
        );
42
      }
43

44
      mustNotBeStarted(start_date ?? quest.start_date);
7✔
45
      mustBeValidDateRange(
6✔
46
        start_date ?? quest.start_date,
12✔
47
        end_date ?? quest.end_date,
11✔
48
      );
49

50
      if (action_metas) {
6!
51
        const c_id = community_id || quest.community_id;
6✔
52
        await Promise.all(
6✔
53
          action_metas.map(async (action_meta) => {
54
            if (action_meta.content_id) {
13✔
55
              // make sure content_id exists
56
              const [content, id] = action_meta.content_id.split(':'); // this has been validated by the schema
1✔
57
              if (content === 'thread') {
1!
58
                const thread = await models.Thread.findOne({
×
59
                  where: c_id ? { id: +id, community_id: c_id } : { id: +id },
×
60
                });
61
                mustExist(`Thread with id "${id}"`, thread);
×
62
              } else if (content === 'comment') {
1!
63
                const comment = await models.Comment.findOne({
1✔
64
                  where: { id: +id },
65
                  include: c_id
1!
66
                    ? [
67
                        {
68
                          model: models.Thread,
69
                          attributes: ['community_id'],
70
                          required: true,
71
                          where: { community_id: c_id },
72
                        },
73
                      ]
74
                    : [],
75
                });
76
                mustExist(`Comment with id "${id}"`, comment);
1✔
77
              }
78
            }
79
          }),
80
        );
81
      }
82

83
      await models.sequelize.transaction(async (transaction) => {
5✔
84
        if (action_metas?.length) {
5!
85
          // clean existing action_metas
86
          await models.QuestActionMeta.destroy({
5✔
87
            where: { quest_id },
88
            transaction,
89
          });
90
          // create new action_metas
91
          await models.QuestActionMeta.bulkCreate(
5✔
92
            action_metas.map((action_meta) => ({
12✔
93
              ...action_meta,
94
              quest_id,
95
            })),
96
          );
97
        }
98

99
        const delta = getDelta(quest, {
5✔
100
          name,
101
          description,
102
          community_id,
103
          image_url,
104
          start_date,
105
          end_date,
106
        });
107
        if (Object.keys(delta).length)
5✔
108
          await models.Quest.update(delta, {
1✔
109
            where: { id: quest_id },
110
            transaction,
111
          });
112
      });
113

114
      const updated = await models.Quest.findOne({
5✔
115
        where: { id: quest_id },
116
        include: { model: models.QuestActionMeta, as: 'action_metas' },
117
      });
118
      return updated!.toJSON();
5✔
119
    },
120
  };
121
}
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

© 2025 Coveralls, Inc