Compare commits
3 Commits
1.5.1-alph
...
developmen
Author | SHA1 | Date |
---|---|---|
|
e92f325807 | |
|
66d76634b7 | |
|
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() {
|
||||
const date = new Date();
|
||||
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 {
|
||||
SalesPriceFormulaEntity,
|
||||
|
@ -10,10 +14,12 @@ import {
|
|||
TransactionSettingModel,
|
||||
} from '../models/sales-price-formula.model';
|
||||
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
|
||||
import { Repository } from 'typeorm';
|
||||
import { MoreThan, Repository } from 'typeorm';
|
||||
import { FormulaType } from '../../constants';
|
||||
import { TaxModel } from 'src/modules/transaction/tax/data/models/tax.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()
|
||||
export class SalesPriceFormulaDataService extends BaseDataService<SalesPriceFormulaEntity> {
|
||||
|
@ -24,6 +30,11 @@ export class SalesPriceFormulaDataService extends BaseDataService<SalesPriceForm
|
|||
private tax: Repository<TaxModel>,
|
||||
@InjectRepository(ItemModel, CONNECTION_NAME.DEFAULT)
|
||||
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);
|
||||
}
|
||||
|
@ -37,11 +48,38 @@ export class SalesPriceFormulaDataService extends BaseDataService<SalesPriceForm
|
|||
}
|
||||
|
||||
async sentToBlackHole() {
|
||||
const percentage = 80;
|
||||
const transactionSettingData = await this.transactionSetting.findOne({
|
||||
where: {},
|
||||
});
|
||||
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) {
|
||||
|
|
|
@ -25,6 +25,7 @@ export class SalesPriceFormulaDataController {
|
|||
return await this.orchestrator.update(data);
|
||||
}
|
||||
|
||||
@Public(true)
|
||||
@Put()
|
||||
async updateTransactionSetting(
|
||||
@Body() data: TransactionSettingDto,
|
||||
|
|
|
@ -16,6 +16,7 @@ export class SalesPriceFormulaReadController {
|
|||
return await this.orchestrator.detail();
|
||||
}
|
||||
|
||||
@Public(true)
|
||||
@Get('detail')
|
||||
async getTransactionSetting(): Promise<any> {
|
||||
return await this.orchestrator.getTransactionSetting();
|
||||
|
|
|
@ -22,13 +22,21 @@ import { TaxDataService } from '../tax/data/services/tax-data.service';
|
|||
import { TaxModel } from '../tax/data/models/tax.model';
|
||||
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
|
||||
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()
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
TypeOrmModule.forFeature(
|
||||
[SalesPriceFormulaModel, TransactionSettingModel, TaxModel, ItemModel],
|
||||
[
|
||||
SalesPriceFormulaModel,
|
||||
TransactionSettingModel,
|
||||
TransactionModel,
|
||||
TaxModel,
|
||||
ItemModel,
|
||||
],
|
||||
CONNECTION_NAME.DEFAULT,
|
||||
),
|
||||
CqrsModule,
|
||||
|
@ -49,6 +57,7 @@ import { UpdateTransactionSettingManager } from './domain/usecases/managers/upda
|
|||
|
||||
SalesPriceFormulaDataOrchestrator,
|
||||
SalesPriceFormulaReadOrchestrator,
|
||||
CouchService,
|
||||
],
|
||||
exports: [SalesPriceFormulaDataService, SalesPriceFormulaReadService],
|
||||
})
|
||||
|
|
|
@ -32,7 +32,10 @@ import { BatchConfirmDataTransactionManager } from './domain/usecases/managers/b
|
|||
import { PosTransactionHandler } from './domain/usecases/handlers/pos-transaction.handler';
|
||||
import { TaxDataService } from '../tax/data/services/tax-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 { SettledTransactionHandler } from './domain/usecases/handlers/settled-transaction.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 { PriceCalculator } from './domain/usecases/calculator/price.calculator';
|
||||
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
|
||||
import { CouchService } from 'src/modules/configuration/couch/data/services/couch.service';
|
||||
|
||||
@Module({
|
||||
exports: [TransactionReadService],
|
||||
|
@ -57,6 +61,7 @@ import { ItemModel } from 'src/modules/item-related/item/data/models/item.model'
|
|||
TransactionTaxModel,
|
||||
TransactionItemTaxModel,
|
||||
TransactionBreakdownTaxModel,
|
||||
TransactionSettingModel,
|
||||
TaxModel,
|
||||
SalesPriceFormulaModel,
|
||||
PaymentMethodModel,
|
||||
|
@ -96,6 +101,8 @@ import { ItemModel } from 'src/modules/item-related/item/data/models/item.model'
|
|||
|
||||
TransactionDataOrchestrator,
|
||||
TransactionReadOrchestrator,
|
||||
|
||||
CouchService,
|
||||
],
|
||||
})
|
||||
export class TransactionModule {}
|
||||
|
|
Loading…
Reference in New Issue