-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
188 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { forwardRef, Module } from '@nestjs/common'; | ||
import { SequelizeModule } from '@nestjs/sequelize'; | ||
import { UsageModel } from './usage.model'; | ||
import { SequelizeUsageRepository } from './usage.repository'; | ||
import { UsageUseCases } from './usage.usecase'; | ||
import { FileModule } from '../file/file.module'; | ||
|
||
@Module({ | ||
imports: [SequelizeModule.forFeature([UsageModel])], | ||
providers: [SequelizeUsageRepository], | ||
exports: [SequelizeUsageRepository], | ||
imports: [ | ||
SequelizeModule.forFeature([UsageModel]), | ||
forwardRef(() => FileModule), | ||
], | ||
providers: [SequelizeUsageRepository, UsageUseCases], | ||
exports: [SequelizeUsageRepository, UsageUseCases, SequelizeModule], | ||
}) | ||
export class PlanModule {} | ||
export class UsageModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { SequelizeUsageRepository } from './usage.repository'; | ||
import { SequelizeFileRepository } from '../file/file.repository'; | ||
import { User } from '../user/user.domain'; | ||
|
||
@Injectable() | ||
export class UsageUseCases { | ||
constructor( | ||
private readonly usageRepository: SequelizeUsageRepository, | ||
private readonly fileRepository: SequelizeFileRepository, | ||
) {} | ||
|
||
async getUserUsage(user: User) { | ||
const userUuid = user.uuid; | ||
|
||
let mostRecentUsage = | ||
await this.usageRepository.getMostRecentUsage(userUuid); | ||
|
||
if (!mostRecentUsage) { | ||
mostRecentUsage = await this.usageRepository.addFirstDailyUsage(userUuid); | ||
} | ||
|
||
const mostRecentUsageNextDay = new Date(mostRecentUsage.period); | ||
mostRecentUsageNextDay.setUTCDate(mostRecentUsageNextDay.getUTCDate() + 1); | ||
|
||
const totalStorageChanged = await this.fileRepository.sumFileSizesSinceDate( | ||
user.id, | ||
mostRecentUsageNextDay, | ||
); | ||
|
||
const totalUsage = await this.usageRepository.getUserUsage(userUuid); | ||
|
||
return { | ||
drive: | ||
totalUsage.total_yearly_delta + | ||
totalUsage.total_monthly_delta + | ||
totalStorageChanged, | ||
id: user.email, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters