Skip to content

Commit 2919836

Browse files
committed
refactor: rename drive_user_uuid to user_id in mail_accounts
- Created a migration to rename the column drive_user_uuid to user_id in the mail_accounts table for improved clarity and consistency. - Updated related code in seeders, services, and repositories to reflect the new user_id field. - Adjusted unit tests to ensure proper functionality with the updated user identification method.
1 parent e46ab72 commit 2919836

8 files changed

Lines changed: 96 additions & 167 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
/** @type {import('sequelize-cli').Migration} */
4+
module.exports = {
5+
async up(queryInterface) {
6+
await queryInterface.renameColumn(
7+
'mail_accounts',
8+
'drive_user_uuid',
9+
'user_id',
10+
);
11+
},
12+
13+
async down(queryInterface) {
14+
await queryInterface.renameColumn(
15+
'mail_accounts',
16+
'user_id',
17+
'drive_user_uuid',
18+
);
19+
},
20+
};

seeders/20260318200321-test-user.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const domain = {
1414
const account = {
1515
id: 'a1b2c3d4-0000-0000-0000-000000000002',
1616
// Matches the uuid of the test user in drive-server-wip seeders
17-
drive_user_uuid: '87204d6b-c4a7-4f38-bd99-f7f47964a643',
17+
user_id: '87204d6b-c4a7-4f38-bd99-f7f47964a643',
1818
created_at: new Date(),
1919
updated_at: new Date(),
2020
};
@@ -49,8 +49,8 @@ module.exports = {
4949
}
5050

5151
const [existingAccount] = await queryInterface.sequelize.query(
52-
'SELECT id FROM mail_accounts WHERE drive_user_uuid = :uuid',
53-
{ replacements: { uuid: account.drive_user_uuid }, type: queryInterface.sequelize.QueryTypes.SELECT },
52+
'SELECT id FROM mail_accounts WHERE user_id = :uuid',
53+
{ replacements: { uuid: account.user_id }, type: queryInterface.sequelize.QueryTypes.SELECT },
5454
);
5555
if (!existingAccount) {
5656
await queryInterface.bulkInsert('mail_accounts', [account]);

src/modules/account/account.service.spec.ts

Lines changed: 36 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,16 @@ describe('AccountService', () => {
4545
it('when account exists, then returns it', async () => {
4646
const attrs = newMailAccountAttributes();
4747
const account = MailAccount.build(attrs);
48-
accounts.findByDriveUserUuid.mockResolvedValue(account);
48+
accounts.findByUserId.mockResolvedValue(account);
4949

50-
const result = await service.getAccount(attrs.driveUserUuid);
50+
const result = await service.getAccount(attrs.userId);
5151

52-
expect(accounts.findByDriveUserUuid).toHaveBeenCalledWith(
53-
attrs.driveUserUuid,
54-
);
52+
expect(accounts.findByUserId).toHaveBeenCalledWith(attrs.userId);
5553
expect(result).toBe(account);
5654
});
5755

5856
it('when account does not exist, then throws NotFoundException', async () => {
59-
accounts.findByDriveUserUuid.mockResolvedValue(null);
57+
accounts.findByUserId.mockResolvedValue(null);
6058

6159
await expect(service.getAccount('unknown-uuid')).rejects.toThrow(
6260
NotFoundException,
@@ -68,36 +66,18 @@ describe('AccountService', () => {
6866
it('when account has a principal name, then deletes from provider and DB', async () => {
6967
const attrs = newMailAccountAttributes();
7068
const account = MailAccount.build(attrs);
71-
accounts.findByDriveUserUuid.mockResolvedValue(account);
69+
accounts.findByUserId.mockResolvedValue(account);
7270

73-
await service.deleteAccount(attrs.driveUserUuid);
71+
await service.deleteAccount(attrs.userId);
7472

7573
expect(provider.deleteAccount).toHaveBeenCalledWith(
7674
account.providerAccountId,
7775
);
7876
expect(accounts.delete).toHaveBeenCalledWith(account.id);
7977
});
8078

81-
it('when account has no principal name, then only deletes from DB', async () => {
82-
const attrs = newMailAccountAttributes({
83-
addresses: [
84-
newMailAddressAttributes({
85-
isDefault: true,
86-
providerExternalId: null,
87-
}),
88-
],
89-
});
90-
const account = MailAccount.build(attrs);
91-
accounts.findByDriveUserUuid.mockResolvedValue(account);
92-
93-
await service.deleteAccount(attrs.driveUserUuid);
94-
95-
expect(provider.deleteAccount).not.toHaveBeenCalled();
96-
expect(accounts.delete).toHaveBeenCalledWith(account.id);
97-
});
98-
9979
it('when account does not exist, then throws NotFoundException', async () => {
100-
accounts.findByDriveUserUuid.mockResolvedValue(null);
80+
accounts.findByUserId.mockResolvedValue(null);
10181

10282
await expect(service.deleteAccount('unknown')).rejects.toThrow(
10383
NotFoundException,
@@ -112,17 +92,15 @@ describe('AccountService', () => {
11292
const domainAttrs = newMailDomainAttributes();
11393
const domain = MailDomain.build(domainAttrs);
11494
const newAddr = 'new@example.com';
115-
const createdAddress = MailAddress.build(
116-
newMailAddressAttributes({ address: newAddr }),
117-
);
95+
const newAddressId = 'new-address-id';
11896

119-
accounts.findByDriveUserUuid.mockResolvedValue(account);
97+
accounts.findByUserId.mockResolvedValue(account);
12098
domains.findByDomain.mockResolvedValue(domain);
12199
addresses.findByAddress.mockResolvedValue(null);
122-
addresses.create.mockResolvedValue(createdAddress);
100+
addresses.create.mockResolvedValue(newAddressId);
123101

124102
await service.addAddress(
125-
accountAttrs.driveUserUuid,
103+
accountAttrs.userId,
126104
newAddr,
127105
domainAttrs.domain,
128106
);
@@ -138,14 +116,14 @@ describe('AccountService', () => {
138116
newAddr,
139117
);
140118
expect(addresses.createProviderLink).toHaveBeenCalledWith({
141-
mailAddressId: createdAddress.id,
119+
mailAddressId: newAddressId,
142120
provider: 'stalwart',
143121
externalId: account.providerAccountId,
144122
});
145123
});
146124

147125
it('when account not found, then throws NotFoundException', async () => {
148-
accounts.findByDriveUserUuid.mockResolvedValue(null);
126+
accounts.findByUserId.mockResolvedValue(null);
149127
domains.findByDomain.mockResolvedValue(
150128
MailDomain.build(newMailDomainAttributes()),
151129
);
@@ -158,12 +136,12 @@ describe('AccountService', () => {
158136

159137
it('when domain not found, then throws NotFoundException', async () => {
160138
const account = MailAccount.build(newMailAccountAttributes());
161-
accounts.findByDriveUserUuid.mockResolvedValue(account);
139+
accounts.findByUserId.mockResolvedValue(account);
162140
domains.findByDomain.mockResolvedValue(null);
163141
addresses.findByAddress.mockResolvedValue(null);
164142

165143
await expect(
166-
service.addAddress(account.driveUserUuid, 'a@b.com', 'unknown.com'),
144+
service.addAddress(account.userId, 'a@b.com', 'unknown.com'),
167145
).rejects.toThrow(NotFoundException);
168146
});
169147

@@ -172,69 +150,33 @@ describe('AccountService', () => {
172150
const domain = MailDomain.build(newMailDomainAttributes());
173151
const existing = MailAddress.build(newMailAddressAttributes());
174152

175-
accounts.findByDriveUserUuid.mockResolvedValue(account);
153+
accounts.findByUserId.mockResolvedValue(account);
176154
domains.findByDomain.mockResolvedValue(domain);
177155
addresses.findByAddress.mockResolvedValue(existing);
178156

179157
await expect(
180-
service.addAddress(
181-
account.driveUserUuid,
182-
existing.address,
183-
domain.domain,
184-
),
158+
service.addAddress(account.userId, existing.address, domain.domain),
185159
).rejects.toThrow(ConflictException);
186160
});
187161

188162
it('when provider fails, then rolls back the created address', async () => {
189163
const account = MailAccount.build(newMailAccountAttributes());
190164
const domain = MailDomain.build(newMailDomainAttributes());
191-
const createdAddress = MailAddress.build(
192-
newMailAddressAttributes({ address: 'new@example.com' }),
193-
);
165+
const newAddressId = 'new-address-id';
194166

195-
accounts.findByDriveUserUuid.mockResolvedValue(account);
167+
accounts.findByUserId.mockResolvedValue(account);
196168
domains.findByDomain.mockResolvedValue(domain);
197169
addresses.findByAddress.mockResolvedValue(null);
198-
addresses.create.mockResolvedValue(createdAddress);
170+
addresses.create.mockResolvedValue(newAddressId);
199171
provider.addAddress.mockRejectedValue(new Error('provider down'));
200172

201173
await expect(
202-
service.addAddress(
203-
account.driveUserUuid,
204-
'new@example.com',
205-
domain.domain,
206-
),
174+
service.addAddress(account.userId, 'new@example.com', domain.domain),
207175
).rejects.toThrow('provider down');
208176

209-
expect(addresses.delete).toHaveBeenCalledWith(createdAddress.id);
177+
expect(addresses.delete).toHaveBeenCalledWith(newAddressId);
210178
expect(addresses.createProviderLink).not.toHaveBeenCalled();
211179
});
212-
213-
it('when account has no principal name, then throws UnprocessableEntityException', async () => {
214-
const account = MailAccount.build(
215-
newMailAccountAttributes({
216-
addresses: [
217-
newMailAddressAttributes({
218-
isDefault: true,
219-
providerExternalId: null,
220-
}),
221-
],
222-
}),
223-
);
224-
const domain = MailDomain.build(newMailDomainAttributes());
225-
226-
accounts.findByDriveUserUuid.mockResolvedValue(account);
227-
domains.findByDomain.mockResolvedValue(domain);
228-
addresses.findByAddress.mockResolvedValue(null);
229-
230-
await expect(
231-
service.addAddress(
232-
account.driveUserUuid,
233-
'new@example.com',
234-
domain.domain,
235-
),
236-
).rejects.toThrow(UnprocessableEntityException);
237-
});
238180
});
239181

240182
describe('removeAddress', () => {
@@ -248,12 +190,9 @@ describe('AccountService', () => {
248190
],
249191
}),
250192
);
251-
accounts.findByDriveUserUuid.mockResolvedValue(account);
193+
accounts.findByUserId.mockResolvedValue(account);
252194

253-
await service.removeAddress(
254-
account.driveUserUuid,
255-
nonDefaultAddr.address,
256-
);
195+
await service.removeAddress(account.userId, nonDefaultAddr.address);
257196

258197
expect(provider.removeAddress).toHaveBeenCalledWith(
259198
account.providerAccountId,
@@ -270,24 +209,24 @@ describe('AccountService', () => {
270209
const account = MailAccount.build(
271210
newMailAccountAttributes({ addresses: [defaultAddr] }),
272211
);
273-
accounts.findByDriveUserUuid.mockResolvedValue(account);
212+
accounts.findByUserId.mockResolvedValue(account);
274213

275214
await expect(
276-
service.removeAddress(account.driveUserUuid, defaultAddr.address),
215+
service.removeAddress(account.userId, defaultAddr.address),
277216
).rejects.toThrow(UnprocessableEntityException);
278217
});
279218

280219
it('when address not found for account, then throws NotFoundException', async () => {
281220
const account = MailAccount.build(newMailAccountAttributes());
282-
accounts.findByDriveUserUuid.mockResolvedValue(account);
221+
accounts.findByUserId.mockResolvedValue(account);
283222

284223
await expect(
285-
service.removeAddress(account.driveUserUuid, 'nonexistent@mail.com'),
224+
service.removeAddress(account.userId, 'nonexistent@mail.com'),
286225
).rejects.toThrow(NotFoundException);
287226
});
288227

289228
it('when account not found, then throws NotFoundException', async () => {
290-
accounts.findByDriveUserUuid.mockResolvedValue(null);
229+
accounts.findByUserId.mockResolvedValue(null);
291230

292231
await expect(service.removeAddress('unknown', 'a@b.com')).rejects.toThrow(
293232
NotFoundException,
@@ -304,54 +243,39 @@ describe('AccountService', () => {
304243
addresses: [defaultAddr, otherAddr],
305244
}),
306245
);
307-
accounts.findByDriveUserUuid.mockResolvedValue(account);
246+
accounts.findByUserId.mockResolvedValue(account);
308247

309-
await service.setPrimaryAddress(account.driveUserUuid, otherAddr.address);
248+
await service.setPrimaryAddress(account.userId, otherAddr.address);
310249

311-
expect(provider.setPrimaryAddress).toHaveBeenCalledWith(
312-
account.providerAccountId,
313-
otherAddr.address,
314-
);
315250
expect(addresses.setDefault).toHaveBeenCalledWith(
316251
otherAddr.id,
317252
account.id,
318253
);
319-
expect(addresses.updateAllProviderExternalIds).toHaveBeenCalledWith(
320-
account.id,
321-
otherAddr.address,
322-
);
323254
});
324255

325256
it('when address is already default, then does nothing', async () => {
326257
const defaultAddr = newMailAddressAttributes({ isDefault: true });
327258
const account = MailAccount.build(
328259
newMailAccountAttributes({ addresses: [defaultAddr] }),
329260
);
330-
accounts.findByDriveUserUuid.mockResolvedValue(account);
261+
accounts.findByUserId.mockResolvedValue(account);
331262

332-
await service.setPrimaryAddress(
333-
account.driveUserUuid,
334-
defaultAddr.address,
335-
);
263+
await service.setPrimaryAddress(account.userId, defaultAddr.address);
336264

337-
expect(provider.setPrimaryAddress).not.toHaveBeenCalled();
338265
expect(addresses.setDefault).not.toHaveBeenCalled();
339266
});
340267

341268
it('when address not found for account, then throws NotFoundException', async () => {
342269
const account = MailAccount.build(newMailAccountAttributes());
343-
accounts.findByDriveUserUuid.mockResolvedValue(account);
270+
accounts.findByUserId.mockResolvedValue(account);
344271

345272
await expect(
346-
service.setPrimaryAddress(
347-
account.driveUserUuid,
348-
'nonexistent@mail.com',
349-
),
273+
service.setPrimaryAddress(account.userId, 'nonexistent@mail.com'),
350274
).rejects.toThrow(NotFoundException);
351275
});
352276

353277
it('when account not found, then throws NotFoundException', async () => {
354-
accounts.findByDriveUserUuid.mockResolvedValue(null);
278+
accounts.findByUserId.mockResolvedValue(null);
355279

356280
await expect(
357281
service.setPrimaryAddress('unknown', 'a@b.com'),

0 commit comments

Comments
 (0)