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

hicommonwealth / commonwealth / 13906683053

17 Mar 2025 06:07PM UTC coverage: 43.626% (+0.007%) from 43.619%
13906683053

Pull #11259

github

web-flow
Merge 03b71931d into 4603c709d
Pull Request #11259: Added in async count cache

1372 of 3506 branches covered (39.13%)

Branch coverage included in aggregate %.

5 of 14 new or added lines in 5 files covered. (35.71%)

75 existing lines in 11 files now uncovered.

2591 of 5578 relevant lines covered (46.45%)

36.97 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 { authVerified } from '../../middleware/auth';
6
import { mustExist } from '../../middleware/guards';
7
import { decodeContent, emitEvent, getDelta, updateTags } from '../../utils';
8

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

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

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

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

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

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

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

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

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

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

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

129
        return updated;
1✔
130
      }
131

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