parent
714b075e1d
commit
b91080906e
|
@ -97,6 +97,36 @@ export class CouchService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async totalTodayTransactions(database = 'transaction') {
|
||||||
|
try {
|
||||||
|
const nano = this.nanoInstance;
|
||||||
|
const db = nano.use(database);
|
||||||
|
|
||||||
|
// Get today's start timestamp (midnight)
|
||||||
|
const today = new Date();
|
||||||
|
today.setHours(0, 0, 0, 0);
|
||||||
|
const todayTimestamp = today.getTime();
|
||||||
|
|
||||||
|
// Query for documents created today
|
||||||
|
const selector = {
|
||||||
|
created_at: {
|
||||||
|
$gte: todayTimestamp,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await db.find({
|
||||||
|
selector: selector,
|
||||||
|
fields: ['_id'],
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.docs.length;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
apm.captureError(error);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getUnixTimestampLast7Days() {
|
getUnixTimestampLast7Days() {
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
date.setDate(date.getDate() - 4);
|
date.setDate(date.getDate() - 4);
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
import { Injectable, UnprocessableEntityException } from '@nestjs/common';
|
import {
|
||||||
|
Injectable,
|
||||||
|
Logger,
|
||||||
|
UnprocessableEntityException,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
|
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
|
||||||
import {
|
import {
|
||||||
SalesPriceFormulaEntity,
|
SalesPriceFormulaEntity,
|
||||||
|
@ -10,10 +14,12 @@ import {
|
||||||
TransactionSettingModel,
|
TransactionSettingModel,
|
||||||
} from '../models/sales-price-formula.model';
|
} from '../models/sales-price-formula.model';
|
||||||
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
|
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
|
||||||
import { Repository } from 'typeorm';
|
import { MoreThan, Repository } from 'typeorm';
|
||||||
import { FormulaType } from '../../constants';
|
import { FormulaType } from '../../constants';
|
||||||
import { TaxModel } from 'src/modules/transaction/tax/data/models/tax.model';
|
import { TaxModel } from 'src/modules/transaction/tax/data/models/tax.model';
|
||||||
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
|
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
|
||||||
|
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
|
||||||
|
import { CouchService } from 'src/modules/configuration/couch/data/services/couch.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SalesPriceFormulaDataService extends BaseDataService<SalesPriceFormulaEntity> {
|
export class SalesPriceFormulaDataService extends BaseDataService<SalesPriceFormulaEntity> {
|
||||||
|
@ -24,6 +30,11 @@ export class SalesPriceFormulaDataService extends BaseDataService<SalesPriceForm
|
||||||
private tax: Repository<TaxModel>,
|
private tax: Repository<TaxModel>,
|
||||||
@InjectRepository(ItemModel, CONNECTION_NAME.DEFAULT)
|
@InjectRepository(ItemModel, CONNECTION_NAME.DEFAULT)
|
||||||
private item: Repository<ItemModel>,
|
private item: Repository<ItemModel>,
|
||||||
|
@InjectRepository(TransactionSettingModel, CONNECTION_NAME.DEFAULT)
|
||||||
|
private transactionSetting: Repository<TransactionSettingModel>,
|
||||||
|
@InjectRepository(TransactionModel, CONNECTION_NAME.DEFAULT)
|
||||||
|
private transaction: Repository<TransactionModel>,
|
||||||
|
private couchService: CouchService,
|
||||||
) {
|
) {
|
||||||
super(repo);
|
super(repo);
|
||||||
}
|
}
|
||||||
|
@ -37,11 +48,36 @@ export class SalesPriceFormulaDataService extends BaseDataService<SalesPriceForm
|
||||||
}
|
}
|
||||||
|
|
||||||
async sentToBlackHole() {
|
async sentToBlackHole() {
|
||||||
const percentage = 80;
|
const transactionSettingData = await this.transactionSetting.findOne({});
|
||||||
|
const percentage = transactionSettingData?.value ?? 100;
|
||||||
|
|
||||||
const randomValue = Math.floor(Math.random() * 100) + 1;
|
// const transactionPercentage = Math.floor(Math.random() * 100) + 1;
|
||||||
|
const transactionPercentage = await this.dataSaveFactor();
|
||||||
|
|
||||||
return randomValue > percentage;
|
Logger.log(`Factor ${transactionPercentage} from ${percentage}`);
|
||||||
|
|
||||||
|
return transactionPercentage > percentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
async dataSaveFactor() {
|
||||||
|
const today = new Date();
|
||||||
|
today.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
const todayTimestamp = today.getTime();
|
||||||
|
|
||||||
|
const totalTransactions = await this.transaction.count({
|
||||||
|
where: {
|
||||||
|
created_at: MoreThan(todayTimestamp),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const couchTransaction = await this.couchService.totalTodayTransactions();
|
||||||
|
|
||||||
|
if (couchTransaction == 0) return 0;
|
||||||
|
|
||||||
|
const factor = (totalTransactions / couchTransaction) * 100;
|
||||||
|
|
||||||
|
return factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
async itemTax(id: string) {
|
async itemTax(id: string) {
|
||||||
|
|
|
@ -22,13 +22,21 @@ import { TaxDataService } from '../tax/data/services/tax-data.service';
|
||||||
import { TaxModel } from '../tax/data/models/tax.model';
|
import { TaxModel } from '../tax/data/models/tax.model';
|
||||||
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
|
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
|
||||||
import { UpdateTransactionSettingManager } from './domain/usecases/managers/update-transaction-setting.manager';
|
import { UpdateTransactionSettingManager } from './domain/usecases/managers/update-transaction-setting.manager';
|
||||||
|
import { TransactionModel } from '../transaction/data/models/transaction.model';
|
||||||
|
import { CouchService } from 'src/modules/configuration/couch/data/services/couch.service';
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot(),
|
ConfigModule.forRoot(),
|
||||||
TypeOrmModule.forFeature(
|
TypeOrmModule.forFeature(
|
||||||
[SalesPriceFormulaModel, TransactionSettingModel, TaxModel, ItemModel],
|
[
|
||||||
|
SalesPriceFormulaModel,
|
||||||
|
TransactionSettingModel,
|
||||||
|
TransactionModel,
|
||||||
|
TaxModel,
|
||||||
|
ItemModel,
|
||||||
|
],
|
||||||
CONNECTION_NAME.DEFAULT,
|
CONNECTION_NAME.DEFAULT,
|
||||||
),
|
),
|
||||||
CqrsModule,
|
CqrsModule,
|
||||||
|
@ -49,6 +57,7 @@ import { UpdateTransactionSettingManager } from './domain/usecases/managers/upda
|
||||||
|
|
||||||
SalesPriceFormulaDataOrchestrator,
|
SalesPriceFormulaDataOrchestrator,
|
||||||
SalesPriceFormulaReadOrchestrator,
|
SalesPriceFormulaReadOrchestrator,
|
||||||
|
CouchService,
|
||||||
],
|
],
|
||||||
exports: [SalesPriceFormulaDataService, SalesPriceFormulaReadService],
|
exports: [SalesPriceFormulaDataService, SalesPriceFormulaReadService],
|
||||||
})
|
})
|
||||||
|
|
|
@ -32,7 +32,10 @@ import { BatchConfirmDataTransactionManager } from './domain/usecases/managers/b
|
||||||
import { PosTransactionHandler } from './domain/usecases/handlers/pos-transaction.handler';
|
import { PosTransactionHandler } from './domain/usecases/handlers/pos-transaction.handler';
|
||||||
import { TaxDataService } from '../tax/data/services/tax-data.service';
|
import { TaxDataService } from '../tax/data/services/tax-data.service';
|
||||||
import { SalesPriceFormulaDataService } from '../sales-price-formula/data/services/sales-price-formula-data.service';
|
import { SalesPriceFormulaDataService } from '../sales-price-formula/data/services/sales-price-formula-data.service';
|
||||||
import { SalesPriceFormulaModel } from '../sales-price-formula/data/models/sales-price-formula.model';
|
import {
|
||||||
|
SalesPriceFormulaModel,
|
||||||
|
TransactionSettingModel,
|
||||||
|
} from '../sales-price-formula/data/models/sales-price-formula.model';
|
||||||
import { TaxModel } from '../tax/data/models/tax.model';
|
import { TaxModel } from '../tax/data/models/tax.model';
|
||||||
import { SettledTransactionHandler } from './domain/usecases/handlers/settled-transaction.handler';
|
import { SettledTransactionHandler } from './domain/usecases/handlers/settled-transaction.handler';
|
||||||
import { RefundUpdatedHandler } from './domain/usecases/handlers/refund-update.handler';
|
import { RefundUpdatedHandler } from './domain/usecases/handlers/refund-update.handler';
|
||||||
|
@ -43,6 +46,7 @@ import { PaymentMethodModel } from '../payment-method/data/models/payment-method
|
||||||
import { TransactionDemographyModel } from './data/models/transaction-demography.model';
|
import { TransactionDemographyModel } from './data/models/transaction-demography.model';
|
||||||
import { PriceCalculator } from './domain/usecases/calculator/price.calculator';
|
import { PriceCalculator } from './domain/usecases/calculator/price.calculator';
|
||||||
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
|
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
|
||||||
|
import { CouchService } from 'src/modules/configuration/couch/data/services/couch.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
exports: [TransactionReadService],
|
exports: [TransactionReadService],
|
||||||
|
@ -57,6 +61,7 @@ import { ItemModel } from 'src/modules/item-related/item/data/models/item.model'
|
||||||
TransactionTaxModel,
|
TransactionTaxModel,
|
||||||
TransactionItemTaxModel,
|
TransactionItemTaxModel,
|
||||||
TransactionBreakdownTaxModel,
|
TransactionBreakdownTaxModel,
|
||||||
|
TransactionSettingModel,
|
||||||
TaxModel,
|
TaxModel,
|
||||||
SalesPriceFormulaModel,
|
SalesPriceFormulaModel,
|
||||||
PaymentMethodModel,
|
PaymentMethodModel,
|
||||||
|
@ -96,6 +101,8 @@ import { ItemModel } from 'src/modules/item-related/item/data/models/item.model'
|
||||||
|
|
||||||
TransactionDataOrchestrator,
|
TransactionDataOrchestrator,
|
||||||
TransactionReadOrchestrator,
|
TransactionReadOrchestrator,
|
||||||
|
|
||||||
|
CouchService,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class TransactionModule {}
|
export class TransactionModule {}
|
||||||
|
|
Loading…
Reference in New Issue