feat: add variable config to formula

feat/fix-couch-transaction
shancheas 2024-08-29 17:19:24 +07:00
parent ce372de1fd
commit e7a7ffb2bc
5 changed files with 101 additions and 2 deletions

View File

@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddValueVariableFormula1724926316235
implements MigrationInterface
{
name = 'AddValueVariableFormula1724926316235';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "price_formulas" ADD "value_for" character varying NOT NULL DEFAULT 'dpp'`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "price_formulas" DROP COLUMN "value_for"`,
);
}
}

View File

@ -11,6 +11,7 @@ import { SalesPriceFormulaEntity } from 'src/modules/transaction/sales-price-for
import { In } from 'typeorm'; import { In } from 'typeorm';
import { STATUS } from 'src/core/strings/constants/base.constants'; import { STATUS } from 'src/core/strings/constants/base.constants';
import { calculateProfitFormula } from 'src/modules/transaction/sales-price-formula/domain/usecases/managers/helpers/calculation-formula.helper'; import { calculateProfitFormula } from 'src/modules/transaction/sales-price-formula/domain/usecases/managers/helpers/calculation-formula.helper';
import { FormulaType } from 'src/modules/transaction/sales-price-formula/constants';
@Injectable() @Injectable()
export class UpdateProfitShareFormulaManager extends BaseUpdateManager<SalesPriceFormulaEntity> { export class UpdateProfitShareFormulaManager extends BaseUpdateManager<SalesPriceFormulaEntity> {
@ -30,6 +31,35 @@ export class UpdateProfitShareFormulaManager extends BaseUpdateManager<SalesPric
} }
async afterProcess(): Promise<void> { async afterProcess(): Promise<void> {
const additionalFormula = this.data.additional;
for (const additional of additionalFormula) {
/**
* Find formula for variable
* If the formula doesn't exist, then create data for save
*/
const formula = (await this.dataService.getOneByOptions({
where: {
value_for: additional.value_for,
},
})) ?? {
editor_id: this.user.id,
editor_name: this.user.name,
updated_at: new Date().getTime(),
created_at: new Date().getTime(),
type: FormulaType.PROFIT_SHARE,
};
// Update formula value to exist formula or new formula
formula.formula_render = additional.formula_render;
formula.formula_string = additional.formula_string;
formula.value_for = additional.value_for;
/**
* This function is create, but inside function is save
* So, if the id is provide, the data will be update instead create new data
*/
this.dataService.create(null, null, formula);
}
return; return;
} }

View File

@ -22,6 +22,9 @@ export class SalesPriceFormulaModel
@Column('varchar', { name: 'formula_string', nullable: true }) @Column('varchar', { name: 'formula_string', nullable: true })
formula_string: string; formula_string: string;
@Column('varchar', { default: 'dpp' })
value_for: string;
@Column('varchar', { name: 'example_formula', nullable: true }) @Column('varchar', { name: 'example_formula', nullable: true })
example_formula: string; example_formula: string;

View File

@ -6,5 +6,13 @@ export interface SalesPriceFormulaEntity extends BaseEntity {
formula_string: string; // digunakan untuk menyimpan string dari formula formula_string: string; // digunakan untuk menyimpan string dari formula
example_formula: string; example_formula: string;
example_result: number; example_result: number;
value_for: string;
type: FormulaType; type: FormulaType;
additional?: AdditionalFormula[];
}
export interface AdditionalFormula {
formula_render: any;
formula_string: string;
value_for: string;
} }

View File

@ -1,11 +1,37 @@
import { BaseDto } from 'src/core/modules/infrastructure/dto/base.dto'; import { BaseDto } from 'src/core/modules/infrastructure/dto/base.dto';
import { SalesPriceFormulaEntity } from '../../domain/entities/sales-price-formula.entity'; import {
AdditionalFormula,
SalesPriceFormulaEntity,
} from '../../domain/entities/sales-price-formula.entity';
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { ValidateIf } from 'class-validator'; import { ValidateIf, ValidateNested } from 'class-validator';
import { Exclude } from 'class-transformer'; import { Exclude } from 'class-transformer';
import { Any } from 'typeorm'; import { Any } from 'typeorm';
import { FormulaType } from '../../constants'; import { FormulaType } from '../../constants';
export class AdditionalFormulaDto implements AdditionalFormula {
@ApiProperty({
type: Any,
required: false,
})
@ValidateIf((body) => body.formula_render)
formula_render: any;
@ApiProperty({
type: String,
required: false,
})
@ValidateIf((body) => body.formula_string)
formula_string: string;
@ApiProperty({
type: String,
required: false,
})
@ValidateIf((body) => body.value_for)
value_for: string;
}
export class SalesPriceFormulaDto export class SalesPriceFormulaDto
extends BaseDto extends BaseDto
implements SalesPriceFormulaEntity implements SalesPriceFormulaEntity
@ -30,6 +56,19 @@ export class SalesPriceFormulaDto
@Exclude() @Exclude()
example_result: number; example_result: number;
@Exclude()
value_for: string;
@Exclude() @Exclude()
type: FormulaType; type: FormulaType;
@ApiProperty({
type: [AdditionalFormulaDto],
default: AdditionalFormulaDto,
})
@ValidateIf(({ additional }) => {
return additional != null;
})
@ValidateNested({ each: true })
additional: AdditionalFormulaDto[];
} }