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

hicommonwealth / commonwealth / 13705230798

06 Mar 2025 06:06PM UTC coverage: 45.409% (-0.3%) from 45.755%
13705230798

Pull #11259

github

web-flow
Merge f98a8572f into 42b2201ae
Pull Request #11259: Added in async count cache

1313 of 3203 branches covered (40.99%)

Branch coverage included in aggregate %.

4 of 15 new or added lines in 4 files covered. (26.67%)

133 existing lines in 11 files now uncovered.

2510 of 5216 relevant lines covered (48.12%)

38.45 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 {
11✔
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
        max_xp_to_end,
28
        action_metas,
29
      } = payload;
11✔
30

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

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

45
      mustNotBeStarted(start_date ?? quest.start_date);
10✔
46
      mustBeValidDateRange(
9✔
47
        start_date ?? quest.start_date,
18✔
48
        end_date ?? quest.end_date,
17✔
49
      );
50

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

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

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

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