mirror of
https://github.com/standardnotes/server
synced 2026-07-14 00:01:54 -04:00
* fix(api-gateway): reduce exports * wip controllers * fix: imports of controllers * fix(api-gateway): rename http service interface to proxy interface * wip: self-registering services and controllers * wip: add registering controller method bindings and services in container * feat: merge two services together * wip: resolving endpoints to direct code calls * wip: bind controller container to a singleton * fix: controller binding to instantiate and self-register on controller container * fix: move signout endpoint to auth controller * wip: define inversify controllers in the controller container * fix(auth): bind inversify controllers to controller container * fix(auth): linter issues * fix(auth): specs * fix(auth): inversify controllers bindings * wip: endpoint resolving * wip: add endpoint for more auth controllers * wip: add sessions controller endpoint resolvings * wip: add subscription invites endpoint resolvings * wip: add subscription tokens endpoint resolvings * wip: add all binding for auth server controllers * wip: fix migrations path * fix: configure default env vars and ci setup
68 lines
2.8 KiB
TypeScript
68 lines
2.8 KiB
TypeScript
import { DomainEventHandlerInterface, FileUploadedEvent } from '@standardnotes/domain-events'
|
|
import { SettingName } from '@standardnotes/settings'
|
|
import { inject, injectable } from 'inversify'
|
|
import { Logger } from 'winston'
|
|
|
|
import TYPES from '../../Bootstrap/Types'
|
|
import { EncryptionVersion } from '../Encryption/EncryptionVersion'
|
|
import { SubscriptionSettingServiceInterface } from '../Setting/SubscriptionSettingServiceInterface'
|
|
import { UserSubscription } from '../Subscription/UserSubscription'
|
|
import { UserSubscriptionServiceInterface } from '../Subscription/UserSubscriptionServiceInterface'
|
|
import { UserRepositoryInterface } from '../User/UserRepositoryInterface'
|
|
|
|
@injectable()
|
|
export class FileUploadedEventHandler implements DomainEventHandlerInterface {
|
|
constructor(
|
|
@inject(TYPES.Auth_UserRepository) private userRepository: UserRepositoryInterface,
|
|
@inject(TYPES.Auth_UserSubscriptionService) private userSubscriptionService: UserSubscriptionServiceInterface,
|
|
@inject(TYPES.Auth_SubscriptionSettingService)
|
|
private subscriptionSettingService: SubscriptionSettingServiceInterface,
|
|
@inject(TYPES.Auth_Logger) private logger: Logger,
|
|
) {}
|
|
|
|
async handle(event: FileUploadedEvent): Promise<void> {
|
|
const user = await this.userRepository.findOneByUuid(event.payload.userUuid)
|
|
if (user === null) {
|
|
this.logger.warn(`Could not find user with uuid: ${event.payload.userUuid}`)
|
|
|
|
return
|
|
}
|
|
|
|
const { regularSubscription, sharedSubscription } =
|
|
await this.userSubscriptionService.findRegularSubscriptionForUserUuid(event.payload.userUuid)
|
|
if (regularSubscription === null) {
|
|
this.logger.warn(`Could not find regular user subscription for user with uuid: ${event.payload.userUuid}`)
|
|
|
|
return
|
|
}
|
|
|
|
await this.updateUploadBytesUsedSetting(regularSubscription, event.payload.fileByteSize)
|
|
|
|
if (sharedSubscription !== null) {
|
|
await this.updateUploadBytesUsedSetting(sharedSubscription, event.payload.fileByteSize)
|
|
}
|
|
}
|
|
|
|
private async updateUploadBytesUsedSetting(subscription: UserSubscription, byteSize: number): Promise<void> {
|
|
let bytesUsed = '0'
|
|
const bytesUsedSetting = await this.subscriptionSettingService.findSubscriptionSettingWithDecryptedValue({
|
|
userUuid: (await subscription.user).uuid,
|
|
userSubscriptionUuid: subscription.uuid,
|
|
subscriptionSettingName: SettingName.create(SettingName.NAMES.FileUploadBytesUsed).getValue(),
|
|
})
|
|
if (bytesUsedSetting !== null) {
|
|
bytesUsed = bytesUsedSetting.value as string
|
|
}
|
|
|
|
await this.subscriptionSettingService.createOrReplace({
|
|
userSubscription: subscription,
|
|
props: {
|
|
name: SettingName.NAMES.FileUploadBytesUsed,
|
|
unencryptedValue: (+bytesUsed + byteSize).toString(),
|
|
sensitive: false,
|
|
serverEncryptionVersion: EncryptionVersion.Unencrypted,
|
|
},
|
|
})
|
|
}
|
|
}
|