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

hicommonwealth / commonwealth / 13771465397

10 Mar 2025 05:56PM UTC coverage: 45.189%. Remained the same
13771465397

push

github

web-flow
Merge pull request #11311 from hicommonwealth/tim/tweet-quests

Quest Tweets and Tweet Metric tracking

1313 of 3225 branches covered (40.71%)

Branch coverage included in aggregate %.

1 of 10 new or added lines in 2 files covered. (10.0%)

2 existing lines in 1 file now uncovered.

2519 of 5255 relevant lines covered (47.94%)

38.4 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
// TODO: use `tweetExists` util to check if `twitter_url` is valid
14
//  once it is added on the client
15
export function UpdateQuest(): Command<typeof schemas.UpdateQuest> {
16
  return {
11✔
17
    ...schemas.UpdateQuest,
18
    auth: [isSuperAdmin],
19
    secure: true,
20
    body: async ({ payload }) => {
21
      const {
22
        quest_id,
23
        name,
24
        description,
25
        community_id,
26
        image_url,
27
        start_date,
28
        end_date,
29
        max_xp_to_end,
30
        action_metas,
31
      } = payload;
11✔
32

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

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

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

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

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

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

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