Skip to content

Commit

Permalink
Simplify upsertion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Jul 25, 2024
1 parent b9354c9 commit 8088fcd
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/domain/notifications/notifications.repository.v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ export class NotificationsRepositoryV2 implements INotificationsRepositoryV2 {
async upsertSubscriptions(args: UpsertSubscriptionsDto): Promise<{
deviceUuid: Uuid;
}> {
const authorizedSafesToSubscribe: UpsertSubscriptionsDto['safes'] = [];

// Only allow owners or delegates to subscribe to notifications
// We don't Promise.all getSafe/getDelegates to prevent unnecessary calls
for (const safeToSubscribe of args.safes) {
Expand All @@ -102,9 +100,8 @@ export class NotificationsRepositoryV2 implements INotificationsRepositoryV2 {
})
.catch(() => null);

// Upsert owner
if (safe && safe.owners.includes(args.account)) {
authorizedSafesToSubscribe.push(safeToSubscribe);
const isOwner = !!safe?.owners.includes(args.account);
if (isOwner) {
continue;
}

Expand All @@ -116,23 +113,17 @@ export class NotificationsRepositoryV2 implements INotificationsRepositoryV2 {
})
.catch(() => null);

// Upsert delegate
if (
delegates &&
delegates.results.some(({ delegate }) => delegate === args.account)
) {
authorizedSafesToSubscribe.push(safeToSubscribe);
const isDelegate = !!delegates?.results.some(
({ delegate }) => delegate === args.account,
);
if (isDelegate) {
continue;
}
}

if (authorizedSafesToSubscribe.length === 0) {
throw new UnauthorizedException();
}

return this.notificationsDatasource.upsertSubscriptions({
...args,
safes: authorizedSafesToSubscribe,
});
return this.notificationsDatasource.upsertSubscriptions(args);
}

getSafeSubscription(args: {
Expand Down

0 comments on commit 8088fcd

Please sign in to comment.