refactor: organize backend by business responsibility
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Environment } from '../../config/env.validation';
|
||||
import { TbankCrypto } from './tbank.crypto';
|
||||
|
||||
@Injectable()
|
||||
export class TbankTokenService {
|
||||
private readonly crypto: TbankCrypto;
|
||||
|
||||
constructor(config: ConfigService<Environment, true>) {
|
||||
this.crypto = TbankCrypto.fromBase64(
|
||||
config.getOrThrow('TOKEN_ENCRYPTION_KEY'),
|
||||
);
|
||||
}
|
||||
|
||||
encrypt(value: string): string {
|
||||
return this.crypto.encrypt(value);
|
||||
}
|
||||
|
||||
decrypt(payload: string): string {
|
||||
return this.crypto.decrypt(payload);
|
||||
}
|
||||
|
||||
async translateInvalidToken<T>(operation: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === 'INVALID_TBANK_TOKEN')
|
||||
throw new BadRequestException({ code: 'INVALID_TBANK_TOKEN' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user