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

hicommonwealth / commonwealth / 13845955660

13 Mar 2025 10:37PM UTC coverage: 43.588% (-1.1%) from 44.638%
13845955660

Pull #11430

github

web-flow
Merge 384ed128b into 54d5b089a
Pull Request #11430: Allow delete contest

1384 of 3534 branches covered (39.16%)

Branch coverage included in aggregate %.

1 of 9 new or added lines in 1 file covered. (11.11%)

219 existing lines in 24 files now uncovered.

2606 of 5620 relevant lines covered (46.37%)

37.7 hits per line

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

73.21
/libs/model/src/aggregates/user/UpdateUser.command.ts
1
import { InvalidInput, type Command } from '@hicommonwealth/core';
2
import * as schemas from '@hicommonwealth/schemas';
3
import { DEFAULT_NAME } from '@hicommonwealth/shared';
4
import { models } from '../../database';
5
import { mustExist } from '../../middleware/guards';
6
import { decodeContent, emitEvent, getDelta, updateTags } from '../../utils';
7

8
export function UpdateUser(): Command<typeof schemas.UpdateUser> {
9
  return {
10
    ...schemas.UpdateUser,
1✔
11
    auth: [],
12
    body: async ({ actor, payload }) => {
13
      // comparing number to string since command convention requires string id
14
      if (actor.user.id != payload.id)
15
        throw new InvalidInput('Invalid user id');
1!
UNCOV
16

×
17
      const { id, profile, tag_ids } = payload;
18
      const {
1✔
19
        slug,
20
        name,
21
        email,
22
        website,
23
        avatar_url,
24
        socials,
25
        bio: rawBio,
26
        background_image,
27
      } = profile;
28

1✔
29
      const user = (
30
        await models.User.findOne({
1✔
31
          where: { id },
32
          include: {
33
            model: models.ProfileTags,
34
          },
35
        })
36
      )?.toJSON();
37
      mustExist('User', user);
38

1✔
39
      const is_welcome_onboard_flow_complete = !!(
40
        user.is_welcome_onboard_flow_complete ||
1✔
41
        (name && name !== DEFAULT_NAME)
3✔
42
      );
43

44
      const promotional_emails_enabled =
45
        payload.promotional_emails_enabled ??
46
        user.promotional_emails_enabled ??
1✔
47
        false;
48

49
      const user_delta = getDelta(user, {
50
        is_welcome_onboard_flow_complete,
1✔
51
        promotional_emails_enabled,
52
        profile: {
53
          email,
54
          slug,
55
          name,
56
          bio: rawBio && decodeContent(rawBio),
57
          website,
1!
58
          avatar_url,
59
          socials,
60
          background_image,
61
        },
62
      });
63

64
      const tags_delta = tag_ids
65
        ? getDelta(
1!
66
            { tag_ids: user.ProfileTags?.map((t) => t.tag_id) },
UNCOV
67
            {
×
68
              tag_ids,
69
            },
70
          )
71
        : {};
72

73
      const update_user = Object.keys(user_delta).length;
74
      const update_tags = Object.keys(tags_delta).length;
1✔
75

1✔
76
      if (update_user || update_tags) {
77
        const updated = await models.sequelize.transaction(
1!
78
          async (transaction) => {
1✔
79
            if (update_tags)
80
              await updateTags(tag_ids!, user.id!, 'user_id', transaction);
1!
UNCOV
81

×
82
            if (update_user) {
83
              // TODO: utility to deep merge deltas
1!
84
              const updates = {
85
                ...user_delta,
1✔
86
                profile: {
87
                  ...user.profile,
88
                  ...user_delta.profile,
89
                  background_image: {
90
                    ...user.profile.background_image,
91
                    ...user_delta.profile?.background_image,
92
                  },
93
                },
94
              };
95
              if (updates.profile.bio === '') {
96
                updates.profile.bio = null;
1!
UNCOV
97
              }
×
98
              const [, rows] = await models.User.update(updates, {
99
                where: { id: user.id },
1✔
100
                returning: true,
101
                transaction,
102
              });
103
              const updated_user = rows.at(0);
104

1✔
105
              // emit sign-up flow completed event when:
106
              if (updated_user && user_delta.is_welcome_onboard_flow_complete) {
107
                await emitEvent(
1!
108
                  models.Outbox,
1✔
109
                  [
110
                    {
111
                      event_name: 'SignUpFlowCompleted',
112
                      event_payload: {
113
                        user_id: id,
114
                        address: actor.address!,
115
                        referred_by_address: user.referred_by_address,
116
                        created_at: updated_user.created_at!,
117
                      },
118
                    },
119
                  ],
120
                  transaction,
121
                );
122
              }
123
              return updated_user;
124
            } else return user;
1✔
UNCOV
125
          },
×
126
        );
127

128
        return updated;
129
      }
1✔
130

131
      // nothing changed
132
      return user;
UNCOV
133
    },
×
134
  };
135
}
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