-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/cvc and ts config #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KiLanger
wants to merge
10
commits into
main
Choose a base branch
from
feature/CVCAndTSConfig
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d3aa290
Linting and strict type rules
9560e59
Updated dto, added CVC logic
d98851a
request fix
b77f17a
Switched back to old ordering logic
d24240e
Improved default orderby
cc99181
throw error to dapr when events fail
05f9128
Fixed issue when payment fails
7f523b5
Moved endpoint definition to env, removed retries for creadit card pa…
d5aded0
Fixed issue with deleting by order
d07dee9
Dynamic exposed variables for retry count
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,18 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { ConfigurationController } from './configuration.controller'; | ||
|
|
||
| describe('ConfigurationController', () => { | ||
| let controller: ConfigurationController; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [ConfigurationController], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<ConfigurationController>(ConfigurationController); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
| }); |
This file contains hidden or 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,29 @@ | ||
| import { Body, Controller, Get, Post } from '@nestjs/common'; | ||
| import { ConfigurationService } from './configuration.service'; | ||
|
|
||
| /** | ||
| * The controller for the payment service. | ||
| */ | ||
| @Controller('ecs') | ||
| export class ConfigurationController { | ||
| constructor(private readonly configurationService: ConfigurationService) {} | ||
|
|
||
| /** | ||
| * Endpoint for service defined variables. | ||
| * @returns The variable definitions as key value pairs. | ||
| */ | ||
| @Get('defined-variables') | ||
| async getDefinedVariables(): Promise<Record<string, any>> { | ||
| return this.configurationService.getDefinedVariables(); | ||
| } | ||
|
|
||
| /** | ||
| * Endpoint to change service variables. | ||
| * @param variables - The updated variables. | ||
| * @returns A promise that resolves to void. | ||
| */ | ||
| @Post('variables') | ||
| async setVariables(@Body() variables: Record<string, any>): Promise<void> { | ||
| return this.configurationService.setVariables(variables); | ||
| } | ||
| } |
This file contains hidden or 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,10 @@ | ||
| import { Logger, Module } from '@nestjs/common'; | ||
| import { ConfigurationController } from './configuration.controller'; | ||
| import { ConfigurationService } from './configuration.service'; | ||
|
|
||
| @Module({ | ||
| controllers: [ConfigurationController], | ||
| providers: [ConfigurationService, Logger], | ||
| exports: [ConfigurationService], | ||
| }) | ||
| export class ConfigurationModule {} |
This file contains hidden or 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,18 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { ConfigurationService } from './configuration.service'; | ||
|
|
||
| describe('ConfigurationService', () => { | ||
| let service: ConfigurationService; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| providers: [ConfigurationService], | ||
| }).compile(); | ||
|
|
||
| service = module.get<ConfigurationService>(ConfigurationService); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(service).toBeDefined(); | ||
| }); | ||
| }); |
This file contains hidden or 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,99 @@ | ||
| import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; | ||
| import { definedVariables } from './variable-definitions/variable-definitions'; | ||
| import { ConfigService } from '@nestjs/config'; | ||
|
|
||
| /** | ||
| * The configuration service for the simulation. | ||
| * It allows exposed variables to be queried and set by the sidecar. | ||
| * It wraps the default nest configService for other, not exposed variables. | ||
| * @property configurations - The internal storage for all exposed variables. | ||
| */ | ||
| @Injectable() | ||
| export class ConfigurationService implements OnModuleInit { | ||
| private configurations = new Map<string, any>(); | ||
|
|
||
| constructor( | ||
| private readonly logger: Logger, | ||
| private readonly configService: ConfigService, | ||
| ) {} | ||
|
|
||
| /** | ||
| * Initializes the module. | ||
| * Sets all defined variables to their default values. | ||
| */ | ||
| onModuleInit() { | ||
| definedVariables.forEach((variable) => { | ||
| const key = Object.keys(variable)[0] as keyof typeof variable; | ||
| const value = variable[key]?.defaultValue; | ||
| if (!value) { | ||
| throw new Error(`Variable ${key} does not have a default value`); | ||
| } | ||
| this.setVariables({ [key]: value }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the service variable definitions. | ||
| * @returns The variable definitions as key value pairs. | ||
| */ | ||
| getDefinedVariables() { | ||
| return definedVariables.reduce((acc: Record<string, any>, current) => { | ||
| const key = Object.keys(current)[0] as keyof typeof current; | ||
| acc[key] = current[key]; | ||
| return acc as Record<string, any>; | ||
| }, {}); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the service variables. | ||
| * Currently supports setting variables of type number, integer, boolean, and string. | ||
| * @param variables - The updated variables. | ||
| */ | ||
| setVariables(variables: Record<string, any>) { | ||
| Object.entries(variables).forEach(([key, value]) => { | ||
| const variable = definedVariables.find((v) => Object.keys(v)[0] === key); | ||
| if (!variable) { | ||
| throw new Error(`Variable ${key} is not defined`); | ||
| } | ||
| const type = Object.values(variable)[0]?.type.type; | ||
| // cast the value to the correct type | ||
| switch (type) { | ||
| case 'number': | ||
| value = Number(value); | ||
| break; | ||
| case 'integer': | ||
| value = parseInt(value, 10); | ||
| break; | ||
| case 'boolean': | ||
| value = value === 'true'; | ||
| break; | ||
| case 'string': | ||
| value = String(value); | ||
| break; | ||
| default: | ||
| throw new Error(`Variable ${key} has an unsupported type ${type}`); | ||
| } | ||
| this.logger.log(`Setting variable ${key} to ${value}`); | ||
| this.configurations.set(key, value); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current value of a variable. | ||
| * @param name - The name of the variable. | ||
| * @param fallback - The value to return if the variable is not defined. | ||
| * @returns The value of the variable with the requested type. | ||
| */ | ||
| getCurrentVariableValue<T>(name: string, fallback: T): T { | ||
| const value = this.configurations.get(name); | ||
| if (value !== undefined) { | ||
| return value as T; | ||
| } | ||
| const envValue = this.configService.get(name); | ||
| if (envValue !== undefined) { | ||
| return envValue as T; | ||
| } | ||
| this.logger.error(`Variable ${name} is not defined`); | ||
| return fallback; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/configuration/variable-definitions/variable-definitions.ts
This file contains hidden or 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,11 @@ | ||
| export const definedVariables = [ | ||
| { | ||
| RETRY_COUNT: { | ||
| type: { | ||
| $schema: 'http://json-schema.org/draft-07/schema#', | ||
| type: 'integer', | ||
| }, | ||
| defaultValue: 3, | ||
| }, | ||
| }, | ||
| ]; |
This file contains hidden or 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,17 +1,23 @@ | ||
| import { Type } from 'class-transformer'; | ||
| import { | ||
| IsDate, | ||
| IsEnum, | ||
| IsOptional, | ||
| IsUUID, | ||
| ValidateNested, | ||
| IsArray, | ||
| IsInt, | ||
| IsDateString, | ||
| IsNumber, | ||
| } from 'class-validator'; | ||
| import { OrderStatus } from './order-status'; | ||
| import { RejectionReason } from './order-rejection-reason'; | ||
| import { OrderItemDTO } from './order-item.dto'; | ||
|
|
||
| export class PaymentAuthorization { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs |
||
| @IsNumber() | ||
| CVC: number; | ||
| } | ||
|
|
||
| /** | ||
| * DTO of an order of a user. | ||
| * | ||
|
|
@@ -26,20 +32,22 @@ import { OrderItemDTO } from './order-item.dto'; | |
| * @property shipmentAddressId UUID of shipment address associated with the Order. | ||
| * @property invoiceAddressId UUID of invoice address associated with the Order. | ||
| * @property paymentInformationId UUID of payment information associated with the Order. | ||
| * @property payment_authorization Optional payment authorization information. | ||
| * @property vat_number VAT number. | ||
| */ | ||
| export class OrderDTO { | ||
| @IsUUID() | ||
| id: string; | ||
| @IsUUID() | ||
| userId: string; | ||
| @IsDate() | ||
| @IsDateString() | ||
| createdAt: Date; | ||
| @IsEnum(OrderStatus) | ||
| orderStatus: OrderStatus; | ||
| @IsInt() | ||
| compensatableOrderAmount: number; | ||
| @IsOptional() | ||
| @IsDate() | ||
| @IsDateString() | ||
| placedAt?: Date; | ||
| @IsOptional() | ||
| @IsEnum(RejectionReason) | ||
|
|
@@ -54,4 +62,8 @@ export class OrderDTO { | |
| invoiceAddressId: string; | ||
| @IsUUID() | ||
| paymentInformationId: string; | ||
| @ValidateNested() | ||
| @Type(() => PaymentAuthorization) | ||
| payment_authorization?: PaymentAuthorization; | ||
| vat_number: string; | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't we discuss to move this to infrastructure-docker instead?