diff --git a/packages/auth/src/Domain/UseCase/CancelSharedSubscriptionInvitation/CancelSharedSubscriptionInvitation.spec.ts b/packages/auth/src/Domain/UseCase/CancelSharedSubscriptionInvitation/CancelSharedSubscriptionInvitation.spec.ts index a706c7330..186c386f2 100644 --- a/packages/auth/src/Domain/UseCase/CancelSharedSubscriptionInvitation/CancelSharedSubscriptionInvitation.spec.ts +++ b/packages/auth/src/Domain/UseCase/CancelSharedSubscriptionInvitation/CancelSharedSubscriptionInvitation.spec.ts @@ -16,6 +16,7 @@ import { DomainEventPublisherInterface, SharedSubscriptionInvitationCanceledEven import { DomainEventFactoryInterface } from '../../Event/DomainEventFactoryInterface' import { InviterIdentifierType } from '../../SharedSubscription/InviterIdentifierType' import { InviteeIdentifierType } from '../../SharedSubscription/InviteeIdentifierType' +import { Logger } from 'winston' describe('CancelSharedSubscriptionInvitation', () => { let sharedSubscriptionInvitationRepository: SharedSubscriptionInvitationRepositoryInterface @@ -28,6 +29,7 @@ describe('CancelSharedSubscriptionInvitation', () => { let invitation: SharedSubscriptionInvitation let domainEventPublisher: DomainEventPublisherInterface let domainEventFactory: DomainEventFactoryInterface + let logger: Logger const createUseCase = () => new CancelSharedSubscriptionInvitation( @@ -38,6 +40,7 @@ describe('CancelSharedSubscriptionInvitation', () => { domainEventPublisher, domainEventFactory, timer, + logger, ) beforeEach(() => { @@ -60,6 +63,9 @@ describe('CancelSharedSubscriptionInvitation', () => { inviteeIdentifierType: InviteeIdentifierType.Email, } as jest.Mocked + logger = {} as jest.Mocked + logger.debug = jest.fn() + sharedSubscriptionInvitationRepository = {} as jest.Mocked sharedSubscriptionInvitationRepository.findOneByUuid = jest.fn().mockReturnValue(invitation) sharedSubscriptionInvitationRepository.save = jest.fn() diff --git a/packages/auth/src/Domain/UseCase/CancelSharedSubscriptionInvitation/CancelSharedSubscriptionInvitation.ts b/packages/auth/src/Domain/UseCase/CancelSharedSubscriptionInvitation/CancelSharedSubscriptionInvitation.ts index 5dfab164f..24db37760 100644 --- a/packages/auth/src/Domain/UseCase/CancelSharedSubscriptionInvitation/CancelSharedSubscriptionInvitation.ts +++ b/packages/auth/src/Domain/UseCase/CancelSharedSubscriptionInvitation/CancelSharedSubscriptionInvitation.ts @@ -2,6 +2,7 @@ import { SubscriptionName } from '@standardnotes/common' import { DomainEventPublisherInterface } from '@standardnotes/domain-events' import { TimerInterface } from '@standardnotes/time' import { inject, injectable } from 'inversify' +import { Logger } from 'winston' import TYPES from '../../../Bootstrap/Types' import { DomainEventFactoryInterface } from '../../Event/DomainEventFactoryInterface' @@ -29,6 +30,7 @@ export class CancelSharedSubscriptionInvitation implements UseCaseInterface { @inject(TYPES.DomainEventPublisher) private domainEventPublisher: DomainEventPublisherInterface, @inject(TYPES.DomainEventFactory) private domainEventFactory: DomainEventFactoryInterface, @inject(TYPES.Timer) private timer: TimerInterface, + @inject(TYPES.Logger) private logger: Logger, ) {} async execute(dto: CancelSharedSubscriptionInvitationDTO): Promise { @@ -36,12 +38,20 @@ export class CancelSharedSubscriptionInvitation implements UseCaseInterface { dto.sharedSubscriptionInvitationUuid, ) if (sharedSubscriptionInvitation === null) { + this.logger.debug( + `Could not find a shared subscription invitation with uuid ${dto.sharedSubscriptionInvitationUuid}`, + ) + return { success: false, } } if (dto.inviterEmail !== sharedSubscriptionInvitation.inviterIdentifier) { + this.logger.debug( + `Subscription belongs to a different inviter (${sharedSubscriptionInvitation.inviterIdentifier}). Modifier: ${dto.inviterEmail}`, + ) + return { success: false, } @@ -53,7 +63,9 @@ export class CancelSharedSubscriptionInvitation implements UseCaseInterface { sharedSubscriptionInvitation.subscriptionId, UserSubscriptionType.Regular, ) - if (inviterUserSubscriptions.length !== 1) { + if (inviterUserSubscriptions.length === 0) { + this.logger.debug(`Could not find a regular subscription with id ${sharedSubscriptionInvitation.subscriptionId}`) + return { success: false, } diff --git a/packages/auth/src/Infra/MySQL/MySQLUserSubscriptionRepository.spec.ts b/packages/auth/src/Infra/MySQL/MySQLUserSubscriptionRepository.spec.ts index 78c0a7b15..a33114a69 100644 --- a/packages/auth/src/Infra/MySQL/MySQLUserSubscriptionRepository.spec.ts +++ b/packages/auth/src/Infra/MySQL/MySQLUserSubscriptionRepository.spec.ts @@ -189,6 +189,7 @@ describe('MySQLUserSubscriptionRepository', () => { ormRepository.createQueryBuilder = jest.fn().mockImplementation(() => selectQueryBuilder) selectQueryBuilder.where = jest.fn().mockReturnThis() + selectQueryBuilder.orderBy = jest.fn().mockReturnThis() selectQueryBuilder.getMany = jest.fn().mockReturnValue([subscription]) const result = await createRepository().findBySubscriptionIdAndType(123, UserSubscriptionType.Regular) @@ -200,6 +201,7 @@ describe('MySQLUserSubscriptionRepository', () => { type: 'regular', }, ) + expect(selectQueryBuilder.orderBy).toHaveBeenCalledWith('created_at', 'DESC') expect(selectQueryBuilder.getMany).toHaveBeenCalled() expect(result).toEqual([subscription]) }) diff --git a/packages/auth/src/Infra/MySQL/MySQLUserSubscriptionRepository.ts b/packages/auth/src/Infra/MySQL/MySQLUserSubscriptionRepository.ts index 7cd63814f..d95678282 100644 --- a/packages/auth/src/Infra/MySQL/MySQLUserSubscriptionRepository.ts +++ b/packages/auth/src/Infra/MySQL/MySQLUserSubscriptionRepository.ts @@ -44,6 +44,7 @@ export class MySQLUserSubscriptionRepository implements UserSubscriptionReposito subscriptionId, type, }) + .orderBy('created_at', 'DESC') .getMany() }