// ... | |
@Transaction() // <-- this | |
async makeRemittanceWithTypeOrmV2(fromId: number, toId: number, sum: number, withError: boolean, @TransactionManager() transactionEntityManager: EntityManager = null /* <-- and this */) { | |
const fromUser = await transactionEntityManager.findOne(User, fromId); | |
const toUser = await transactionEntityManager.findOne(User, toId); | |
if (fromUser === undefined) { | |
throw new Error(NOT_FOUND_USER_WITH_ID(fromId)); | |
} | |
if (toUser === undefined) { | |
throw new Error(NOT_FOUND_USER_WITH_ID(toId)); | |
} | |
if (fromUser.defaultPurseId === null) { | |
throw new Error(USER_DOES_NOT_HAVE_PURSE(fromId)); | |
} | |
if (toUser.defaultPurseId === null) { | |
throw new Error(USER_DOES_NOT_HAVE_PURSE(toId)); | |
} | |
const fromPurse = await transactionEntityManager.findOne(Purse, fromUser.defaultPurseId); | |
const toPurse = await transactionEntityManager.findOne(Purse, toUser.defaultPurseId); | |
const modalSum = Math.abs(sum); | |
if (fromPurse.balance < modalSum) { | |
throw new Error(NOT_ENOUGH_MONEY(fromId)); | |
} | |
fromPurse.balance -= sum; | |
toPurse.balance += sum; | |
await this.appServiceV2.savePurseInTransaction(fromPurse, transactionEntityManager); // <-- we will check is it will working | |
if (withError) { | |
throw new Error('Unexpectable error was thrown while remittance'); | |
} | |
await transactionEntityManager.save(toPurse); | |
const remittance = new RemittanceResultDto(); | |
remittance.fromId = fromId; | |
remittance.toId = toId; | |
remittance.fromBalance = fromPurse.balance; | |
remittance.sum = sum; | |
return remittance; | |
} | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment