Compare commits

...

7 Commits

5 changed files with 34 additions and 24 deletions

View File

@ -17,8 +17,8 @@ export abstract class BaseDataService<Entity> {
entityTarget: EntityTarget<Entity>,
entity: Entity,
): Promise<Entity> {
const newEntity = queryRunner.manager.create(entityTarget, entity);
return await queryRunner.manager.save(newEntity);
// const newEntity = this.repository.create(entityTarget, entity);
return await this.repository.save(entity);
}
async createMany(
@ -26,8 +26,8 @@ export abstract class BaseDataService<Entity> {
entityTarget: EntityTarget<Entity>,
entity: Entity[],
): Promise<Entity[]> {
const newEntity = queryRunner.manager.create(entityTarget, entity);
return await queryRunner.manager.save(newEntity);
// const newEntity = this.repository.create(entityTarget, entity);
return await this.repository.save(entity);
}
async createBatch(
@ -35,8 +35,8 @@ export abstract class BaseDataService<Entity> {
entityTarget: EntityTarget<Entity>,
entity: Entity[],
): Promise<Entity[]> {
const newEntity = queryRunner.manager.create(entityTarget, entity);
return await queryRunner.manager.save(newEntity);
// const newEntity = this.repository.create(entityTarget, entity);
return await this.repository.save(entity);
}
async update(
@ -45,13 +45,13 @@ export abstract class BaseDataService<Entity> {
filterUpdate: any,
entity: Entity,
): Promise<Entity> {
const newEntity = await queryRunner.manager.findOne(entityTarget, {
const newEntity = await this.repository.findOne({
where: filterUpdate,
});
if (!newEntity) throw new Error('Data not found!');
Object.assign(newEntity, entity);
return await queryRunner.manager.save(newEntity);
return await this.repository.save(newEntity);
}
async deleteById(
@ -59,7 +59,7 @@ export abstract class BaseDataService<Entity> {
entityTarget: EntityTarget<Entity>,
id: string,
): Promise<void> {
await queryRunner.manager.delete(entityTarget, { id });
await this.repository.delete(id);
}
async deleteByOptions(
@ -67,11 +67,8 @@ export abstract class BaseDataService<Entity> {
entityTarget: EntityTarget<Entity>,
findManyOptions: FindManyOptions<Entity>,
): Promise<void> {
const datas = await queryRunner.manager.find(entityTarget, findManyOptions);
await queryRunner.manager.delete(
entityTarget,
datas?.map((item) => item['id']),
);
const datas = await this.repository.find(findManyOptions);
await this.repository.delete(datas?.map((item) => item['id']));
}
async getOneByOptions(findOneOptions): Promise<Entity> {

View File

@ -60,7 +60,7 @@ export abstract class BaseManager {
try {
this.setUser();
// this.queryRunner.startTransaction();
this.queryRunner.startTransaction();
this.baseLog.verbose('prepareData');
await this.prepareData();
@ -80,13 +80,13 @@ export abstract class BaseManager {
this.baseLog.verbose('afterProcess');
await this.afterProcess();
// this.baseLog.verbose('commitTransaction');
// await this.queryRunner.commitTransaction();
// await this.queryRunner.release();
this.baseLog.verbose('commitTransaction');
await this.queryRunner.commitTransaction();
} catch (e) {
if (e.response) throw new Error(JSON.stringify(e.response));
else throw new Error(e.message);
} finally {
await this.queryRunner.release();
}
}

View File

@ -17,10 +17,17 @@ import { STATUS } from 'src/core/strings/constants/base.constants';
@Injectable()
export class BatchCancelRefundManager extends BaseBatchUpdateStatusManager<RefundEntity> {
validateData(data: RefundEntity): Promise<void> {
if (![STATUS.REFUNDED, STATUS.PENDING].includes(data.status)) {
if (
![
STATUS.REFUNDED,
STATUS.PENDING,
STATUS.PROCESS_REFUND,
STATUS.PARTIAL_REFUND,
].includes(data.status)
) {
throw new UnprocessableEntityException({
statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
message: `Failed! only data with status ${STATUS.REFUNDED} and ${STATUS.PENDING} can be cancelled`,
message: `Failed! only data with status ${STATUS.REFUNDED}, ${STATUS.PROCESS_REFUND} , ${STATUS.PARTIAL_REFUND} and ${STATUS.PENDING} can be cancelled`,
error: 'Unprocessable Entity',
});
}

View File

@ -20,10 +20,17 @@ export class CancelRefundManager extends BaseUpdateStatusManager<RefundEntity> {
}
async validateProcess(): Promise<void> {
if (![STATUS.REFUNDED, STATUS.PENDING].includes(this.data.status)) {
if (
![
STATUS.REFUNDED,
STATUS.PENDING,
STATUS.PROCESS_REFUND,
STATUS.PARTIAL_REFUND,
].includes(this.data.status)
) {
throw new UnprocessableEntityException({
statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
message: `Failed! only data with status ${STATUS.REFUNDED} and ${STATUS.PENDING} can be cancelled`,
message: `Failed! only data with status ${STATUS.REFUNDED}, ${STATUS.PROCESS_REFUND} , ${STATUS.PARTIAL_REFUND} and ${STATUS.PENDING} can be cancelled`,
error: 'Unprocessable Entity',
});
}

View File

@ -42,7 +42,6 @@ export class UpdateTransactionManager extends BaseUpdateManager<TransactionEntit
return [
{
topic: TransactionUpdatedEvent,
data: this.data,
},
];
}