-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: make settings tests independent * feat: implement upsert * test: upsert * fix: update schemas * fix: tests
- Loading branch information
1 parent
c1c9e0a
commit 00d2bba
Showing
5 changed files
with
135 additions
and
17 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
49 changes: 49 additions & 0 deletions
49
packages/runtime/src/useCases/consumption/settings/UpsertSettingByKey.ts
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,49 @@ | ||
import { Serializable } from "@js-soft/ts-serval"; | ||
import { Result } from "@js-soft/ts-utils"; | ||
import { SettingsController } from "@nmshd/consumption"; | ||
import { AccountController } from "@nmshd/transport"; | ||
import { Inject } from "typescript-ioc"; | ||
import { SettingDTO } from "../../../types"; | ||
import { SchemaRepository, SchemaValidator, UseCase } from "../../common"; | ||
import { SettingMapper } from "./SettingMapper"; | ||
|
||
export interface UpsertSettingByKeyRequest { | ||
key: string; | ||
value: any; | ||
} | ||
|
||
class Validator extends SchemaValidator<UpsertSettingByKeyRequest> { | ||
public constructor(@Inject schemaRepository: SchemaRepository) { | ||
super(schemaRepository.getSchema("UpsertSettingByKeyRequest")); | ||
} | ||
} | ||
|
||
export class UpsertSettingByKeyUseCase extends UseCase<UpsertSettingByKeyRequest, SettingDTO> { | ||
public constructor( | ||
@Inject private readonly settingController: SettingsController, | ||
@Inject private readonly accountController: AccountController, | ||
@Inject validator: Validator | ||
) { | ||
super(validator); | ||
} | ||
|
||
protected async executeInternal(request: UpsertSettingByKeyRequest): Promise<Result<SettingDTO>> { | ||
const settings = await this.settingController.getSettings({ key: request.key }); | ||
|
||
const newValue = Serializable.fromUnknown(request.value); | ||
|
||
if (settings.length === 0) { | ||
const setting = await this.settingController.createSetting({ key: request.key, value: newValue }); | ||
await this.accountController.syncDatawallet(); | ||
return Result.ok(SettingMapper.toSettingDTO(setting)); | ||
} | ||
|
||
const latestSetting = settings.reduce((prev, current) => (prev.createdAt > current.createdAt ? prev : current)); | ||
|
||
latestSetting.value = newValue; | ||
await this.settingController.updateSetting(latestSetting); | ||
await this.accountController.syncDatawallet(); | ||
|
||
return Result.ok(SettingMapper.toSettingDTO(latestSetting)); | ||
} | ||
} |
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