Skip to content

Commit e643952

Browse files
committed
feat: do not allow linking non existent subscriptions
Signed-off-by: Alexander Alemayhu <alexander@alemayhu.com>
1 parent b2ca021 commit e643952

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/controllers/UsersControllers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ class UsersController {
211211
}
212212

213213
try {
214+
const emailExists =
215+
await this.userService.checkSubscriptionEmailExists(email);
216+
if (!emailExists) {
217+
console.warn('Linking attempted with non-existent email');
218+
return res.status(400).json({ message: 'Failed to link email.' });
219+
}
220+
214221
await this.userService.updateSubscriptionLinkedEmail(owner, email);
215222
return res.status(200).json({});
216223
} catch (error) {

src/data_layer/UsersRepository.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ class UsersRepository {
106106
updatePatreonByEmail(email: string, patreon: boolean) {
107107
return this.database(this.table).where({ email }).update({ patreon });
108108
}
109+
110+
async checkSubscriptionEmailExists(email: string): Promise<boolean> {
111+
const subscription = await this.database('subscriptions')
112+
.where({ email: email.toLowerCase() })
113+
.first();
114+
return !!subscription;
115+
}
109116
}
110117

111118
export default UsersRepository;

src/services/UsersService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ class UsersService {
6969
return this.repository.getSubscriptionLinkedEmail(owner);
7070
}
7171

72+
async checkSubscriptionEmailExists(email: string): Promise<boolean> {
73+
const subscription =
74+
await this.repository.checkSubscriptionEmailExists(email);
75+
return !!subscription;
76+
}
77+
7278
getUserById(owner: string): Promise<Users> {
7379
return this.repository.getById(owner);
7480
}

0 commit comments

Comments
 (0)