69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
import { EntityNotFoundError, Repository } from 'typeorm';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { CommissionSetting } from './entities/commission_setting.entity';
|
|
|
|
@Injectable()
|
|
export class CommissionService {
|
|
constructor(
|
|
@InjectRepository(CommissionSetting)
|
|
private commissionRepository: Repository<CommissionSetting>,
|
|
) {}
|
|
|
|
findAllCommission(page, pageSize?) {
|
|
return this.commissionRepository.findAndCount({
|
|
skip: page * (pageSize || 10),
|
|
take: pageSize || 10,
|
|
order: {
|
|
version: 'DESC',
|
|
},
|
|
});
|
|
}
|
|
|
|
async findOne(role: string) {
|
|
try {
|
|
return await this.commissionRepository.findOneOrFail({
|
|
where: {
|
|
role: role,
|
|
},
|
|
});
|
|
} catch (e) {
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'Commission not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
async updateCommission(id: string, request) {
|
|
try {
|
|
await this.commissionRepository.findOneOrFail(id);
|
|
} catch (e) {
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'Commission not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
const result = await this.commissionRepository.update(id, {
|
|
commission: request.value,
|
|
});
|
|
|
|
return this.commissionRepository.findOneOrFail(id);
|
|
}
|
|
}
|