feat(auth): add processing user requests

This commit is contained in:
Karol Sójko 2022-11-02 13:23:15 +01:00
parent f2415527f0
commit 2255f856f9
No known key found for this signature in database
GPG key ID: A50543BF560BDEB0
32 changed files with 443 additions and 93 deletions

83
.pnp.cjs generated
View file

@ -2555,16 +2555,16 @@ const RAW_RUNTIME_STATE =
}]\
]],\
["@standardnotes/api", [\
["npm:1.17.2", {\
"packageLocation": "./.yarn/cache/@standardnotes-api-npm-1.17.2-c07102ac18-4f74f52306.zip/node_modules/@standardnotes/api/",\
["npm:1.19.0", {\
"packageLocation": "./.yarn/cache/@standardnotes-api-npm-1.19.0-ce8296df3c-b28884be40.zip/node_modules/@standardnotes/api/",\
"packageDependencies": [\
["@standardnotes/api", "npm:1.17.2"],\
["@standardnotes/api", "npm:1.19.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/encryption", "npm:1.18.5"],\
["@standardnotes/models", "npm:1.30.0"],\
["@standardnotes/responses", "npm:1.11.2"],\
["@standardnotes/encryption", "npm:1.19.0"],\
["@standardnotes/models", "npm:1.33.0"],\
["@standardnotes/responses", "npm:1.12.0"],\
["@standardnotes/security", "workspace:packages/security"],\
["@standardnotes/utils", "npm:1.10.0"],\
["@standardnotes/utils", "npm:1.11.0"],\
["reflect-metadata", "npm:0.1.13"]\
],\
"linkType": "HARD"\
@ -2635,7 +2635,7 @@ const RAW_RUNTIME_STATE =
["@newrelic/winston-enricher", "virtual:04783e12400851b8a3d76e71495851cc94959db6e62f04cb0a31190080629440b182d8c8eb4d7f2b04e281912f2783a5fd4d2c3c6ab68d38b7097246c93f4c19#npm:4.0.0"],\
["@sentry/node", "npm:7.5.0"],\
["@standardnotes/analytics", "workspace:packages/analytics"],\
["@standardnotes/api", "npm:1.17.2"],\
["@standardnotes/api", "npm:1.19.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/domain-events", "workspace:packages/domain-events"],\
["@standardnotes/domain-events-infra", "workspace:packages/domain-events-infra"],\
@ -2662,7 +2662,7 @@ const RAW_RUNTIME_STATE =
["axios", "npm:0.27.2"],\
["bcryptjs", "npm:2.4.3"],\
["cors", "npm:2.8.5"],\
["dayjs", "npm:1.11.5"],\
["dayjs", "npm:1.11.6"],\
["dotenv", "npm:16.0.1"],\
["eslint", "npm:8.25.0"],\
["eslint-plugin-prettier", "virtual:04783e12400851b8a3d76e71495851cc94959db6e62f04cb0a31190080629440b182d8c8eb4d7f2b04e281912f2783a5fd4d2c3c6ab68d38b7097246c93f4c19#npm:4.2.1"],\
@ -2764,15 +2764,15 @@ const RAW_RUNTIME_STATE =
}]\
]],\
["@standardnotes/encryption", [\
["npm:1.18.5", {\
"packageLocation": "./.yarn/cache/@standardnotes-encryption-npm-1.18.5-80059dd62d-b023188012.zip/node_modules/@standardnotes/encryption/",\
["npm:1.19.0", {\
"packageLocation": "./.yarn/cache/@standardnotes-encryption-npm-1.19.0-29799e7bcc-af7665e979.zip/node_modules/@standardnotes/encryption/",\
"packageDependencies": [\
["@standardnotes/encryption", "npm:1.18.5"],\
["@standardnotes/encryption", "npm:1.19.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/models", "npm:1.30.0"],\
["@standardnotes/responses", "npm:1.11.2"],\
["@standardnotes/models", "npm:1.33.0"],\
["@standardnotes/responses", "npm:1.12.0"],\
["@standardnotes/sncrypto-common", "npm:1.13.0"],\
["@standardnotes/utils", "npm:1.10.0"],\
["@standardnotes/utils", "npm:1.11.0"],\
["reflect-metadata", "npm:0.1.13"]\
],\
"linkType": "HARD"\
@ -2821,10 +2821,10 @@ const RAW_RUNTIME_STATE =
],\
"linkType": "HARD"\
}],\
["npm:1.53.2", {\
"packageLocation": "./.yarn/cache/@standardnotes-features-npm-1.53.2-cd8faaf4ee-820219e3a5.zip/node_modules/@standardnotes/features/",\
["npm:1.54.0", {\
"packageLocation": "./.yarn/cache/@standardnotes-features-npm-1.54.0-15f8cf20f0-7647e7506e.zip/node_modules/@standardnotes/features/",\
"packageDependencies": [\
["@standardnotes/features", "npm:1.53.2"],\
["@standardnotes/features", "npm:1.54.0"],\
["@standardnotes/auth", "npm:3.19.4"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/security", "workspace:packages/security"],\
@ -2860,7 +2860,7 @@ const RAW_RUNTIME_STATE =
["aws-sdk", "npm:2.1234.0"],\
["connect-busboy", "npm:1.0.0"],\
["cors", "npm:2.8.5"],\
["dayjs", "npm:1.11.5"],\
["dayjs", "npm:1.11.6"],\
["dotenv", "npm:16.0.1"],\
["eslint", "npm:8.25.0"],\
["eslint-plugin-prettier", "virtual:04783e12400851b8a3d76e71495851cc94959db6e62f04cb0a31190080629440b182d8c8eb4d7f2b04e281912f2783a5fd4d2c3c6ab68d38b7097246c93f4c19#npm:4.2.1"],\
@ -2900,14 +2900,14 @@ const RAW_RUNTIME_STATE =
],\
"linkType": "HARD"\
}],\
["npm:1.30.0", {\
"packageLocation": "./.yarn/cache/@standardnotes-models-npm-1.30.0-c2134137c4-40c234f92b.zip/node_modules/@standardnotes/models/",\
["npm:1.33.0", {\
"packageLocation": "./.yarn/cache/@standardnotes-models-npm-1.33.0-df7d20ae6d-7d45409e4a.zip/node_modules/@standardnotes/models/",\
"packageDependencies": [\
["@standardnotes/models", "npm:1.30.0"],\
["@standardnotes/models", "npm:1.33.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/features", "npm:1.53.2"],\
["@standardnotes/responses", "npm:1.11.2"],\
["@standardnotes/utils", "npm:1.10.0"],\
["@standardnotes/features", "npm:1.54.0"],\
["@standardnotes/responses", "npm:1.12.0"],\
["@standardnotes/utils", "npm:1.11.0"],\
["lodash", "npm:4.17.21"],\
["reflect-metadata", "npm:0.1.13"]\
],\
@ -2955,12 +2955,12 @@ const RAW_RUNTIME_STATE =
],\
"linkType": "HARD"\
}],\
["npm:1.11.2", {\
"packageLocation": "./.yarn/cache/@standardnotes-responses-npm-1.11.2-61f5ee52c3-2a1ffd142c.zip/node_modules/@standardnotes/responses/",\
["npm:1.12.0", {\
"packageLocation": "./.yarn/cache/@standardnotes-responses-npm-1.12.0-1de721974f-15b2e92d57.zip/node_modules/@standardnotes/responses/",\
"packageDependencies": [\
["@standardnotes/responses", "npm:1.11.2"],\
["@standardnotes/responses", "npm:1.12.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/features", "npm:1.53.2"],\
["@standardnotes/features", "npm:1.54.0"],\
["@standardnotes/security", "workspace:packages/security"],\
["reflect-metadata", "npm:0.1.13"]\
],\
@ -2985,7 +2985,7 @@ const RAW_RUNTIME_STATE =
["@types/node", "npm:18.0.3"],\
["@typescript-eslint/eslint-plugin", "virtual:04783e12400851b8a3d76e71495851cc94959db6e62f04cb0a31190080629440b182d8c8eb4d7f2b04e281912f2783a5fd4d2c3c6ab68d38b7097246c93f4c19#npm:5.40.1"],\
["aws-sdk", "npm:2.1234.0"],\
["dayjs", "npm:1.11.5"],\
["dayjs", "npm:1.11.6"],\
["dotenv", "npm:16.0.1"],\
["eslint", "npm:8.25.0"],\
["eslint-plugin-prettier", "virtual:04783e12400851b8a3d76e71495851cc94959db6e62f04cb0a31190080629440b182d8c8eb4d7f2b04e281912f2783a5fd4d2c3c6ab68d38b7097246c93f4c19#npm:4.2.1"],\
@ -3158,7 +3158,7 @@ const RAW_RUNTIME_STATE =
["@types/jest", "npm:29.1.1"],\
["@types/microtime", "npm:2.1.0"],\
["@typescript-eslint/eslint-plugin", "virtual:c66bf20e88479ada0172094776519a9f51acc4731d22079b60a295bcec7ea42d5545cbce58a77a50d932bf953298799135e99707486e343da6d99ba1d167bdbd#npm:5.30.5"],\
["dayjs", "npm:1.11.5"],\
["dayjs", "npm:1.11.6"],\
["eslint-plugin-prettier", "virtual:c66bf20e88479ada0172094776519a9f51acc4731d22079b60a295bcec7ea42d5545cbce58a77a50d932bf953298799135e99707486e343da6d99ba1d167bdbd#npm:4.2.1"],\
["jest", "virtual:c66bf20e88479ada0172094776519a9f51acc4731d22079b60a295bcec7ea42d5545cbce58a77a50d932bf953298799135e99707486e343da6d99ba1d167bdbd#npm:29.1.2"],\
["microtime", "npm:3.1.1"],\
@ -3180,6 +3180,17 @@ const RAW_RUNTIME_STATE =
["reflect-metadata", "npm:0.1.13"]\
],\
"linkType": "HARD"\
}],\
["npm:1.11.0", {\
"packageLocation": "./.yarn/cache/@standardnotes-utils-npm-1.11.0-afbc24024c-9e7d9c1257.zip/node_modules/@standardnotes/utils/",\
"packageDependencies": [\
["@standardnotes/utils", "npm:1.11.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["dompurify", "npm:2.4.0"],\
["lodash", "npm:4.17.21"],\
["reflect-metadata", "npm:0.1.13"]\
],\
"linkType": "HARD"\
}]\
]],\
["@standardnotes/websockets-server", [\
@ -3189,7 +3200,7 @@ const RAW_RUNTIME_STATE =
["@standardnotes/websockets-server", "workspace:packages/websockets"],\
["@newrelic/winston-enricher", "virtual:04783e12400851b8a3d76e71495851cc94959db6e62f04cb0a31190080629440b182d8c8eb4d7f2b04e281912f2783a5fd4d2c3c6ab68d38b7097246c93f4c19#npm:4.0.0"],\
["@sentry/node", "npm:7.5.0"],\
["@standardnotes/api", "npm:1.17.2"],\
["@standardnotes/api", "npm:1.19.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/domain-events", "workspace:packages/domain-events"],\
["@standardnotes/domain-events-infra", "workspace:packages/domain-events-infra"],\
@ -3229,7 +3240,7 @@ const RAW_RUNTIME_STATE =
["@standardnotes/workspace-server", "workspace:packages/workspace"],\
["@newrelic/winston-enricher", "virtual:04783e12400851b8a3d76e71495851cc94959db6e62f04cb0a31190080629440b182d8c8eb4d7f2b04e281912f2783a5fd4d2c3c6ab68d38b7097246c93f4c19#npm:4.0.0"],\
["@sentry/node", "npm:7.5.0"],\
["@standardnotes/api", "npm:1.17.2"],\
["@standardnotes/api", "npm:1.19.0"],\
["@standardnotes/common", "workspace:packages/common"],\
["@standardnotes/domain-events", "workspace:packages/domain-events"],\
["@standardnotes/domain-events-infra", "workspace:packages/domain-events-infra"],\
@ -5891,10 +5902,10 @@ const RAW_RUNTIME_STATE =
}]\
]],\
["dayjs", [\
["npm:1.11.5", {\
"packageLocation": "./.yarn/cache/dayjs-npm-1.11.5-a825142dc5-ea78d43de0.zip/node_modules/dayjs/",\
["npm:1.11.6", {\
"packageLocation": "./.yarn/cache/dayjs-npm-1.11.6-44daf311a9-f59ea45f24.zip/node_modules/dayjs/",\
"packageDependencies": [\
["dayjs", "npm:1.11.5"]\
["dayjs", "npm:1.11.6"]\
],\
"linkType": "HARD"\
}]\

View file

@ -147,4 +147,9 @@ export class UsersController extends BaseHttpController {
async deleteUser(request: Request, response: Response): Promise<void> {
await this.httpService.callPaymentsServer(request, response, 'api/account', request.body)
}
@httpPost('/:userUuid/requests', TYPES.AuthMiddleware)
async submitRequest(request: Request, response: Response): Promise<void> {
await this.httpService.callAuthServer(request, response, `users/${request.params.userUuid}/requests`, request.body)
}
}

View file

@ -20,6 +20,7 @@ import '../src/Controller/SubscriptionSettingsController'
import '../src/Infra/InversifyExpressUtils/InversifyExpressAuthController'
import '../src/Infra/InversifyExpressUtils/InversifyExpressSubscriptionInvitesController'
import '../src/Infra/InversifyExpressUtils/InversifyExpressUserRequestsController'
import '../src/Infra/InversifyExpressUtils/InversifyExpressWebSocketsController'
import * as cors from 'cors'

View file

@ -33,7 +33,7 @@
"@newrelic/winston-enricher": "^4.0.0",
"@sentry/node": "^7.3.0",
"@standardnotes/analytics": "workspace:*",
"@standardnotes/api": "^1.17.2",
"@standardnotes/api": "^1.19.0",
"@standardnotes/common": "workspace:*",
"@standardnotes/domain-events": "workspace:*",
"@standardnotes/domain-events-infra": "workspace:*",
@ -49,7 +49,7 @@
"axios": "^0.27.2",
"bcryptjs": "2.4.3",
"cors": "2.8.5",
"dayjs": "^1.11.3",
"dayjs": "^1.11.6",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"inversify": "^6.0.1",

View file

@ -203,6 +203,8 @@ import { PaymentSuccessEventHandler } from '../Domain/Handler/PaymentSuccessEven
import { RefundProcessedEventHandler } from '../Domain/Handler/RefundProcessedEventHandler'
import { SubscriptionInvitesController } from '../Controller/SubscriptionInvitesController'
import { CreateCrossServiceToken } from '../Domain/UseCase/CreateCrossServiceToken/CreateCrossServiceToken'
import { ProcessUserRequest } from '../Domain/UseCase/ProcessUserRequest/ProcessUserRequest'
import { UserRequestsController } from '../Controller/UserRequestsController'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const newrelicFormatter = require('@newrelic/winston-enricher')
@ -266,6 +268,7 @@ export class ContainerConfigLoader {
// Controller
container.bind<AuthController>(TYPES.AuthController).to(AuthController)
container.bind<SubscriptionInvitesController>(TYPES.SubscriptionInvitesController).to(SubscriptionInvitesController)
container.bind<UserRequestsController>(TYPES.UserRequestsController).to(UserRequestsController)
// Repositories
container.bind<SessionRepositoryInterface>(TYPES.SessionRepository).to(MySQLSessionRepository)
@ -438,6 +441,7 @@ export class ContainerConfigLoader {
container.bind<GetUserAnalyticsId>(TYPES.GetUserAnalyticsId).to(GetUserAnalyticsId)
container.bind<VerifyPredicate>(TYPES.VerifyPredicate).to(VerifyPredicate)
container.bind<CreateCrossServiceToken>(TYPES.CreateCrossServiceToken).to(CreateCrossServiceToken)
container.bind<ProcessUserRequest>(TYPES.ProcessUserRequest).to(ProcessUserRequest)
// Handlers
container.bind<UserRegisteredEventHandler>(TYPES.UserRegisteredEventHandler).to(UserRegisteredEventHandler)

View file

@ -6,6 +6,7 @@ const TYPES = {
// Controller
AuthController: Symbol.for('AuthController'),
SubscriptionInvitesController: Symbol.for('SubscriptionInvitesController'),
UserRequestsController: Symbol.for('UserRequestsController'),
// Repositories
UserRepository: Symbol.for('UserRepository'),
SessionRepository: Symbol.for('SessionRepository'),
@ -121,6 +122,7 @@ const TYPES = {
GetUserAnalyticsId: Symbol.for('GetUserAnalyticsId'),
VerifyPredicate: Symbol.for('VerifyPredicate'),
CreateCrossServiceToken: Symbol.for('CreateCrossServiceToken'),
ProcessUserRequest: Symbol.for('ProcessUserRequest'),
// Handlers
UserRegisteredEventHandler: Symbol.for('UserRegisteredEventHandler'),
AccountDeletionRequestedEventHandler: Symbol.for('AccountDeletionRequestedEventHandler'),

View file

@ -109,4 +109,15 @@ describe('AuthController', () => {
expect(response.status).toEqual(400)
})
it('should throw error on the delete user method as it is still a part of the payments server', async () => {
let caughtError = null
try {
await createController().deleteAccount({ userUuid: '1-2-3' })
} catch (error) {
caughtError = error
}
expect(caughtError).not.toBeNull()
})
})

View file

@ -2,6 +2,7 @@ import { inject, injectable } from 'inversify'
import { DomainEventPublisherInterface } from '@standardnotes/domain-events'
import {
HttpStatusCode,
UserDeletionResponse,
UserRegistrationRequestParams,
UserRegistrationResponse,
UserServerInterface,
@ -12,6 +13,7 @@ import { ClearLoginAttempts } from '../Domain/UseCase/ClearLoginAttempts'
import { Register } from '../Domain/UseCase/Register'
import { DomainEventFactoryInterface } from '../Domain/Event/DomainEventFactoryInterface'
import { ProtocolVersion } from '@standardnotes/common'
import { UserDeletionRequestParams } from '@standardnotes/api/dist/Domain/Request/User/UserDeletionRequestParams'
@injectable()
export class AuthController implements UserServerInterface {
@ -22,6 +24,10 @@ export class AuthController implements UserServerInterface {
@inject(TYPES.DomainEventFactory) private domainEventFactory: DomainEventFactoryInterface,
) {}
async deleteAccount(_params: UserDeletionRequestParams): Promise<UserDeletionResponse> {
throw new Error('This method is implemented on the payments server.')
}
async register(params: UserRegistrationRequestParams): Promise<UserRegistrationResponse> {
if (!params.email || !params.password) {
return {

View file

@ -0,0 +1,43 @@
import 'reflect-metadata'
import { UserRequestType } from '@standardnotes/common'
import { ProcessUserRequest } from '../Domain/UseCase/ProcessUserRequest/ProcessUserRequest'
import { UserRequestsController } from './UserRequestsController'
describe('UserRequestsController', () => {
let processUserRequest: ProcessUserRequest
const createController = () => new UserRequestsController(processUserRequest)
beforeEach(() => {
processUserRequest = {} as jest.Mocked<ProcessUserRequest>
processUserRequest.execute = jest.fn().mockReturnValue({ success: true })
})
it('should process user request', async () => {
expect(
await createController().submitUserRequest({
userUuid: '1-2-3',
requestType: UserRequestType.ExitDiscount,
}),
).toEqual({
status: 200,
data: { success: true },
})
})
it('should not process user request', async () => {
processUserRequest.execute = jest.fn().mockReturnValue({ success: false })
expect(
await createController().submitUserRequest({
userUuid: '1-2-3',
requestType: UserRequestType.ExitDiscount,
}),
).toEqual({
status: 400,
data: { success: false },
})
})
})

View file

@ -0,0 +1,34 @@
import {
HttpStatusCode,
UserRequestRequestParams,
UserRequestResponse,
UserRequestServerInterface,
} from '@standardnotes/api'
import { inject, injectable } from 'inversify'
import TYPES from '../Bootstrap/Types'
import { ProcessUserRequest } from '../Domain/UseCase/ProcessUserRequest/ProcessUserRequest'
@injectable()
export class UserRequestsController implements UserRequestServerInterface {
constructor(@inject(TYPES.ProcessUserRequest) private processUserRequest: ProcessUserRequest) {}
async submitUserRequest(params: UserRequestRequestParams): Promise<UserRequestResponse> {
const result = await this.processUserRequest.execute({
requestType: params.requestType,
userEmail: params.userEmail as string,
userUuid: params.userUuid,
})
if (!result.success) {
return {
status: HttpStatusCode.BadRequest,
data: result,
}
}
return {
status: HttpStatusCode.Success,
data: result,
}
}
}

View file

@ -18,6 +18,29 @@ describe('DomainEventFactory', () => {
timer.getUTCDate = jest.fn().mockReturnValue(new Date(1))
})
it('should create a EXIT_DISCOUNT_APPLY_REQUESTED event', () => {
expect(
createFactory().createExitDiscountApplyRequestedEvent({
userEmail: 'test@test.te',
discountCode: 'exit-20',
}),
).toEqual({
createdAt: expect.any(Date),
meta: {
correlation: {
userIdentifier: 'test@test.te',
userIdentifierType: 'email',
},
origin: 'auth',
},
payload: {
userEmail: 'test@test.te',
discountCode: 'exit-20',
},
type: 'EXIT_DISCOUNT_APPLY_REQUESTED',
})
})
it('should create a WEB_SOCKET_MESSAGE_REQUESTED event', () => {
expect(
createFactory().createWebSocketMessageRequestedEvent({

View file

@ -16,6 +16,7 @@ import {
DomainEventService,
EmailMessageRequestedEvent,
WebSocketMessageRequestedEvent,
ExitDiscountApplyRequestedEvent,
} from '@standardnotes/domain-events'
import { Predicate, PredicateVerificationResult } from '@standardnotes/predicates'
import { TimerInterface } from '@standardnotes/time'
@ -28,6 +29,24 @@ import { DomainEventFactoryInterface } from './DomainEventFactoryInterface'
export class DomainEventFactory implements DomainEventFactoryInterface {
constructor(@inject(TYPES.Timer) private timer: TimerInterface) {}
createExitDiscountApplyRequestedEvent(dto: {
userEmail: string
discountCode: string
}): ExitDiscountApplyRequestedEvent {
return {
type: 'EXIT_DISCOUNT_APPLY_REQUESTED',
createdAt: this.timer.getUTCDate(),
meta: {
correlation: {
userIdentifier: dto.userEmail,
userIdentifierType: 'email',
},
origin: DomainEventService.Auth,
},
payload: dto,
}
}
createWebSocketMessageRequestedEvent(dto: { userUuid: Uuid; message: JSONString }): WebSocketMessageRequestedEvent {
return {
type: 'WEB_SOCKET_MESSAGE_REQUESTED',

View file

@ -16,6 +16,7 @@ import {
PredicateVerifiedEvent,
EmailMessageRequestedEvent,
WebSocketMessageRequestedEvent,
ExitDiscountApplyRequestedEvent,
} from '@standardnotes/domain-events'
import { InviteeIdentifierType } from '../SharedSubscription/InviteeIdentifierType'
@ -83,4 +84,8 @@ export interface DomainEventFactoryInterface {
predicate: Predicate
predicateVerificationResult: PredicateVerificationResult
}): PredicateVerifiedEvent
createExitDiscountApplyRequestedEvent(dto: {
userEmail: string
discountCode: string
}): ExitDiscountApplyRequestedEvent
}

View file

@ -0,0 +1,94 @@
import 'reflect-metadata'
import { DomainEventPublisherInterface, ExitDiscountApplyRequestedEvent } from '@standardnotes/domain-events'
import { UserRequestType } from '@standardnotes/common'
import { DomainEventFactoryInterface } from '../../Event/DomainEventFactoryInterface'
import { UserSubscription } from '../../Subscription/UserSubscription'
import { UserSubscriptionRepositoryInterface } from '../../Subscription/UserSubscriptionRepositoryInterface'
import { ProcessUserRequest } from './ProcessUserRequest'
describe('ProcessUserRequest', () => {
let userSubscriptionRepository: UserSubscriptionRepositoryInterface
let domainEventFactory: DomainEventFactoryInterface
let domainEventPublisher: DomainEventPublisherInterface
const createUseCase = () =>
new ProcessUserRequest(userSubscriptionRepository, domainEventFactory, domainEventPublisher)
beforeEach(() => {
userSubscriptionRepository = {} as jest.Mocked<UserSubscriptionRepositoryInterface>
userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue({
cancelled: true,
} as jest.Mocked<UserSubscription>)
domainEventFactory = {} as jest.Mocked<DomainEventFactoryInterface>
domainEventFactory.createExitDiscountApplyRequestedEvent = jest
.fn()
.mockReturnValue({} as jest.Mocked<ExitDiscountApplyRequestedEvent>)
domainEventPublisher = {} as jest.Mocked<DomainEventPublisherInterface>
domainEventPublisher.publish = jest.fn()
})
it('should not process unsupported requests', async () => {
expect(
await createUseCase().execute({
userEmail: 'test@test.te',
userUuid: '1-2-3',
requestType: 'foobar' as UserRequestType,
}),
).toEqual({
success: false,
})
expect(domainEventPublisher.publish).not.toHaveBeenCalled()
})
it('should not process uncancelled subscriptions', async () => {
userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue({} as jest.Mocked<UserSubscription>)
expect(
await createUseCase().execute({
userEmail: 'test@test.te',
userUuid: '1-2-3',
requestType: UserRequestType.ExitDiscount,
}),
).toEqual({
success: false,
})
expect(domainEventPublisher.publish).not.toHaveBeenCalled()
})
it('should not process non existing subscriptions', async () => {
userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue(null)
expect(
await createUseCase().execute({
userEmail: 'test@test.te',
userUuid: '1-2-3',
requestType: UserRequestType.ExitDiscount,
}),
).toEqual({
success: false,
})
expect(domainEventPublisher.publish).not.toHaveBeenCalled()
})
it('should publish an exit discount apply requested event', async () => {
expect(
await createUseCase().execute({
userEmail: 'test@test.te',
userUuid: '1-2-3',
requestType: UserRequestType.ExitDiscount,
}),
).toEqual({
success: true,
})
expect(domainEventPublisher.publish).toHaveBeenCalled()
})
})

View file

@ -0,0 +1,46 @@
import { DomainEventPublisherInterface } from '@standardnotes/domain-events'
import { inject, injectable } from 'inversify'
import { UserRequestType } from '@standardnotes/common'
import TYPES from '../../../Bootstrap/Types'
import { DomainEventFactoryInterface } from '../../Event/DomainEventFactoryInterface'
import { UserSubscriptionRepositoryInterface } from '../../Subscription/UserSubscriptionRepositoryInterface'
import { UseCaseInterface } from '../UseCaseInterface'
import { ProcessUserRequestDTO } from './ProcessUserRequestDTO'
import { ProcessUserRequestResponse } from './ProcessUserRequestResponse'
@injectable()
export class ProcessUserRequest implements UseCaseInterface {
constructor(
@inject(TYPES.UserSubscriptionRepository) private userSubscriptionRepository: UserSubscriptionRepositoryInterface,
@inject(TYPES.DomainEventFactory) private domainEventFactory: DomainEventFactoryInterface,
@inject(TYPES.DomainEventPublisher) private domainEventPublisher: DomainEventPublisherInterface,
) {}
async execute(dto: ProcessUserRequestDTO): Promise<ProcessUserRequestResponse> {
if (dto.requestType !== UserRequestType.ExitDiscount) {
return {
success: false,
}
}
const subscription = await this.userSubscriptionRepository.findOneByUserUuid(dto.userUuid)
if (subscription === null || !subscription.cancelled) {
return {
success: false,
}
}
await this.domainEventPublisher.publish(
this.domainEventFactory.createExitDiscountApplyRequestedEvent({
userEmail: dto.userEmail,
discountCode: 'exit-20',
}),
)
return {
success: true,
}
}
}

View file

@ -0,0 +1,7 @@
import { UserRequestType, Uuid } from '@standardnotes/common'
export type ProcessUserRequestDTO = {
userUuid: Uuid
userEmail: string
requestType: UserRequestType
}

View file

@ -0,0 +1,3 @@
export type ProcessUserRequestResponse = {
success: boolean
}

View file

@ -0,0 +1,24 @@
import { Request, Response } from 'express'
import { inject } from 'inversify'
import { controller, BaseHttpController, results, httpPost } from 'inversify-express-utils'
import TYPES from '../../Bootstrap/Types'
import { UserRequestsController } from '../../Controller/UserRequestsController'
@controller('/users/:userUuid/requests')
export class InversifyExpressAuthController extends BaseHttpController {
constructor(@inject(TYPES.UserRequestsController) private userRequestsController: UserRequestsController) {
super()
}
@httpPost('/', TYPES.ApiGatewayAuthMiddleware)
async submitRequest(request: Request, response: Response): Promise<results.JsonResult> {
const result = await this.userRequestsController.submitUserRequest({
requestType: request.body.requestType,
userUuid: response.locals.user.uuid,
userEmail: response.locals.user.email,
})
return this.json(result.data, result.status)
}
}

View file

@ -36,7 +36,7 @@
"aws-sdk": "^2.1158.0",
"connect-busboy": "^1.0.0",
"cors": "^2.8.5",
"dayjs": "^1.11.3",
"dayjs": "^1.11.6",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-winston": "^4.0.5",

View file

@ -32,7 +32,7 @@
"@standardnotes/predicates": "workspace:*",
"@standardnotes/time": "workspace:*",
"aws-sdk": "^2.1158.0",
"dayjs": "^1.11.3",
"dayjs": "^1.11.6",
"dotenv": "^16.0.1",
"inversify": "^6.0.1",
"ioredis": "^5.2.0",

View file

@ -23,7 +23,7 @@
"test": "jest spec --coverage"
},
"dependencies": {
"dayjs": "^1.10.8",
"dayjs": "^1.11.6",
"microtime": "^3.1.0",
"reflect-metadata": "^0.1.13"
},

View file

@ -35,9 +35,9 @@ describe('Timer', () => {
})
it('should calculate days difference between now and a given date', () => {
const dateNDaysAgo = createTimer().getUTCDateNDaysAgo(4)
const dateNDaysAgo = createTimer().getUTCDateNDaysAgo(3)
expect(createTimer().dateWasNDaysAgo(dateNDaysAgo)).toEqual(4)
expect(createTimer().dateWasNDaysAgo(dateNDaysAgo)).toEqual(3)
})
it('should return a utc date n hours ago', () => {

View file

@ -24,7 +24,7 @@
"dependencies": {
"@newrelic/winston-enricher": "^4.0.0",
"@sentry/node": "^7.3.0",
"@standardnotes/api": "^1.17.2",
"@standardnotes/api": "^1.19.0",
"@standardnotes/common": "workspace:^",
"@standardnotes/domain-events": "workspace:^",
"@standardnotes/domain-events-infra": "workspace:^",

View file

@ -24,7 +24,7 @@
"dependencies": {
"@newrelic/winston-enricher": "^4.0.0",
"@sentry/node": "^7.3.0",
"@standardnotes/api": "^1.17.2",
"@standardnotes/api": "^1.19.0",
"@standardnotes/common": "workspace:*",
"@standardnotes/domain-events": "workspace:^",
"@standardnotes/domain-events-infra": "workspace:^",

108
yarn.lock
View file

@ -1861,18 +1861,18 @@ __metadata:
languageName: unknown
linkType: soft
"@standardnotes/api@npm:^1.17.2":
version: 1.17.2
resolution: "@standardnotes/api@npm:1.17.2"
"@standardnotes/api@npm:^1.19.0":
version: 1.19.0
resolution: "@standardnotes/api@npm:1.19.0"
dependencies:
"@standardnotes/common": "npm:^1.39.0"
"@standardnotes/encryption": "npm:1.18.5"
"@standardnotes/models": "npm:1.30.0"
"@standardnotes/responses": "npm:1.11.2"
"@standardnotes/common": "npm:^1.43.0"
"@standardnotes/encryption": "npm:1.19.0"
"@standardnotes/models": "npm:1.33.0"
"@standardnotes/responses": "npm:1.12.0"
"@standardnotes/security": "npm:^1.1.0"
"@standardnotes/utils": "npm:1.10.0"
"@standardnotes/utils": "npm:1.11.0"
reflect-metadata: "npm:^0.1.13"
checksum: 4f74f52306c27f8bc8b26eb78abb952393b608f16cf18683ec3f7363b64c511786401e59fd57bfc7613c55876aabcb47f256dfcdb0d09b8416ca1901d8562338
checksum: b28884be401012f9bac25639f240f80179f59c01d03258979fc89793e26d1b4303752ef15c150bd5b76227676b10ce1ff6fea498f92fd03be6918d08eec8097b
languageName: node
linkType: hard
@ -1883,7 +1883,7 @@ __metadata:
"@newrelic/winston-enricher": "npm:^4.0.0"
"@sentry/node": "npm:^7.3.0"
"@standardnotes/analytics": "workspace:*"
"@standardnotes/api": "npm:^1.17.2"
"@standardnotes/api": "npm:^1.19.0"
"@standardnotes/common": "workspace:*"
"@standardnotes/domain-events": "workspace:*"
"@standardnotes/domain-events-infra": "workspace:*"
@ -1910,7 +1910,7 @@ __metadata:
axios: "npm:^0.27.2"
bcryptjs: "npm:2.4.3"
cors: "npm:2.8.5"
dayjs: "npm:^1.11.3"
dayjs: "npm:^1.11.6"
dotenv: "npm:^16.0.1"
eslint: "npm:^8.14.0"
eslint-plugin-prettier: "npm:^4.0.0"
@ -1945,7 +1945,7 @@ __metadata:
languageName: node
linkType: hard
"@standardnotes/common@npm:^1.19.1, @standardnotes/common@npm:^1.23.1, @standardnotes/common@npm:^1.39.0, @standardnotes/common@workspace:*, @standardnotes/common@workspace:^, @standardnotes/common@workspace:packages/common":
"@standardnotes/common@npm:^1.19.1, @standardnotes/common@npm:^1.23.1, @standardnotes/common@npm:^1.39.0, @standardnotes/common@npm:^1.43.0, @standardnotes/common@workspace:*, @standardnotes/common@workspace:^, @standardnotes/common@workspace:packages/common":
version: 0.0.0-use.local
resolution: "@standardnotes/common@workspace:packages/common"
dependencies:
@ -2013,17 +2013,17 @@ __metadata:
languageName: unknown
linkType: soft
"@standardnotes/encryption@npm:1.18.5":
version: 1.18.5
resolution: "@standardnotes/encryption@npm:1.18.5"
"@standardnotes/encryption@npm:1.19.0":
version: 1.19.0
resolution: "@standardnotes/encryption@npm:1.19.0"
dependencies:
"@standardnotes/common": "npm:^1.39.0"
"@standardnotes/models": "npm:1.30.0"
"@standardnotes/responses": "npm:1.11.2"
"@standardnotes/common": "npm:^1.43.0"
"@standardnotes/models": "npm:1.33.0"
"@standardnotes/responses": "npm:1.12.0"
"@standardnotes/sncrypto-common": "npm:1.13.0"
"@standardnotes/utils": "npm:1.10.0"
"@standardnotes/utils": "npm:1.11.0"
reflect-metadata: "npm:^0.1.13"
checksum: b02318801254b428d6a9a26f8ab9bf9b47ddf4e538616f928e6b8db6dfa60270e99b9cd350346d7956c923071ac693d6952c5ec195e46f3df1256a9996520a2f
checksum: af7665e97983650978462f65c1ba768f2c03b742497e3256957beb2e0dcf1d8c47f34f8778eb2dd52c0c7345f0b86f701ca64e0b8fd4ea6597b83c5b73ca7f5b
languageName: node
linkType: hard
@ -2068,15 +2068,15 @@ __metadata:
languageName: node
linkType: hard
"@standardnotes/features@npm:1.53.2":
version: 1.53.2
resolution: "@standardnotes/features@npm:1.53.2"
"@standardnotes/features@npm:1.54.0":
version: 1.54.0
resolution: "@standardnotes/features@npm:1.54.0"
dependencies:
"@standardnotes/auth": "npm:^3.19.4"
"@standardnotes/common": "npm:^1.39.0"
"@standardnotes/common": "npm:^1.43.0"
"@standardnotes/security": "npm:^1.2.0"
reflect-metadata: "npm:^0.1.13"
checksum: 820219e3a58fb2b03f3341e9cac2c4a5704b77f1ec8f1ad986aa7ffa92e2ce4bc87c24a1aee98ac80c957a293edf84bef91cff9fce973ab2f108f5ef726db2cc
checksum: 7647e7506e2d863c8a8909644aca157baa9b3b23f0fe260d1786ba8366267571e414ddf5000e1fe109b918d979e97428b1d57afbd457db49efd4219b4c8ff759
languageName: node
linkType: hard
@ -2106,7 +2106,7 @@ __metadata:
aws-sdk: "npm:^2.1158.0"
connect-busboy: "npm:^1.0.0"
cors: "npm:^2.8.5"
dayjs: "npm:^1.11.3"
dayjs: "npm:^1.11.6"
dotenv: "npm:^16.0.1"
eslint: "npm:^8.14.0"
eslint-plugin-prettier: "npm:^4.0.0"
@ -2131,17 +2131,17 @@ __metadata:
languageName: unknown
linkType: soft
"@standardnotes/models@npm:1.30.0":
version: 1.30.0
resolution: "@standardnotes/models@npm:1.30.0"
"@standardnotes/models@npm:1.33.0":
version: 1.33.0
resolution: "@standardnotes/models@npm:1.33.0"
dependencies:
"@standardnotes/common": "npm:^1.39.0"
"@standardnotes/features": "npm:1.53.2"
"@standardnotes/responses": "npm:1.11.2"
"@standardnotes/utils": "npm:1.10.0"
"@standardnotes/common": "npm:^1.43.0"
"@standardnotes/features": "npm:1.54.0"
"@standardnotes/responses": "npm:1.12.0"
"@standardnotes/utils": "npm:1.11.0"
lodash: "npm:^4.17.21"
reflect-metadata: "npm:^0.1.13"
checksum: 40c234f92ba0b4c33ba7e8cc604058f3b87086d83dd5642ed5d17faff47dacbf6079bf3912c5eac6f51d0dae13da1ad960746c3cc36a0d37128f3bf6b2ce873d
checksum: 7d45409e4aeef5d4299e1fd12a18e13554023e63652460754313099d11f54130afa4bf8f0de0280ed7a83ff6bb4bc59eb96b28a4f1f617755e969f4672b07b1a
languageName: node
linkType: hard
@ -2197,15 +2197,15 @@ __metadata:
languageName: node
linkType: hard
"@standardnotes/responses@npm:1.11.2":
version: 1.11.2
resolution: "@standardnotes/responses@npm:1.11.2"
"@standardnotes/responses@npm:1.12.0":
version: 1.12.0
resolution: "@standardnotes/responses@npm:1.12.0"
dependencies:
"@standardnotes/common": "npm:^1.39.0"
"@standardnotes/features": "npm:1.53.2"
"@standardnotes/common": "npm:^1.43.0"
"@standardnotes/features": "npm:1.54.0"
"@standardnotes/security": "npm:^1.1.0"
reflect-metadata: "npm:^0.1.13"
checksum: 2a1ffd142c23866228afe06d1dc6e453515d8bf8ad3da0ca2e24d638835a2f639913d75aa830c24343bed743baeb76c382c2cf70232e75538b5c3803fe8ddf0d
checksum: 15b2e92d57870d881b2923843db6c9506be6f1e49e4522cbc261a8c8933bff2a41b0e82855fd5e8c42f933da9f05408f38e2f1d53b320a61312ee76bac37398c
languageName: node
linkType: hard
@ -2226,7 +2226,7 @@ __metadata:
"@types/node": "npm:^18.0.0"
"@typescript-eslint/eslint-plugin": "npm:^5.29.0"
aws-sdk: "npm:^2.1158.0"
dayjs: "npm:^1.11.3"
dayjs: "npm:^1.11.6"
dotenv: "npm:^16.0.1"
eslint: "npm:^8.14.0"
eslint-plugin-prettier: "npm:^4.0.0"
@ -2386,7 +2386,7 @@ __metadata:
"@types/jest": "npm:^29.1.1"
"@types/microtime": "npm:^2.1.0"
"@typescript-eslint/eslint-plugin": "npm:^5.30.0"
dayjs: "npm:^1.10.8"
dayjs: "npm:^1.11.6"
eslint-plugin-prettier: "npm:^4.2.1"
jest: "npm:^29.1.2"
microtime: "npm:^3.1.0"
@ -2408,13 +2408,25 @@ __metadata:
languageName: node
linkType: hard
"@standardnotes/utils@npm:1.11.0":
version: 1.11.0
resolution: "@standardnotes/utils@npm:1.11.0"
dependencies:
"@standardnotes/common": "npm:^1.43.0"
dompurify: "npm:^2.3.8"
lodash: "npm:^4.17.21"
reflect-metadata: "npm:^0.1.13"
checksum: 9e7d9c12573d9819f8dfa3128d5437e7c6c7cad72e6c65e90456a1dfcfe711fb9fdc91493fb974fa6941a37cbd62a800283d4b04d8a6464250a1681da7ace983
languageName: node
linkType: hard
"@standardnotes/websockets-server@workspace:packages/websockets":
version: 0.0.0-use.local
resolution: "@standardnotes/websockets-server@workspace:packages/websockets"
dependencies:
"@newrelic/winston-enricher": "npm:^4.0.0"
"@sentry/node": "npm:^7.3.0"
"@standardnotes/api": "npm:^1.17.2"
"@standardnotes/api": "npm:^1.19.0"
"@standardnotes/common": "workspace:^"
"@standardnotes/domain-events": "workspace:^"
"@standardnotes/domain-events-infra": "workspace:^"
@ -2452,7 +2464,7 @@ __metadata:
dependencies:
"@newrelic/winston-enricher": "npm:^4.0.0"
"@sentry/node": "npm:^7.3.0"
"@standardnotes/api": "npm:^1.17.2"
"@standardnotes/api": "npm:^1.19.0"
"@standardnotes/common": "workspace:*"
"@standardnotes/domain-events": "workspace:^"
"@standardnotes/domain-events-infra": "workspace:^"
@ -4551,10 +4563,10 @@ __metadata:
languageName: node
linkType: hard
"dayjs@npm:^1.10.8, dayjs@npm:^1.11.3":
version: 1.11.5
resolution: "dayjs@npm:1.11.5"
checksum: ea78d43de0ff67d65f54cd8c927908ee72421f4d73ce5aa19060e20f26b1940db2980b273427eb1813434fdb2df6731e2ac609d97e89e665d29e7b638f762894
"dayjs@npm:^1.11.6":
version: 1.11.6
resolution: "dayjs@npm:1.11.6"
checksum: f59ea45f2438056f10955a979124738906d897fb642b6157ead34b675240a79a1424655f691a35af810248575506459bf65eadd7d51625cdc537bf805a92dea6
languageName: node
linkType: hard