你好,我正在尝试写一段代码,从mongo db上的某个帐户文档中移除金钱,并将其贷记到另一个帐户中。在这样做的过程中,我创建了一个事务,我似乎无法理解解决这个问题的逻辑。
const Transactions = require("../models/transaction");
const Accounts = require("../models/account");
const { StatusCodes } = require("http-status-codes");
const { BadRequestError, NotFoundError } = require("../errors");
/**
* Credits an account by an amount
*
* @param {String} account_number the account number of the account to be credited
* @param {Number} amount the amount to be credited
*/
const credit = async (account_number, amount) => {
return await Accounts.findOneAndUpdate(
{ account_number },
{ $inc: { account_balance: amount } },
{ new: true }
);
};
/**
* Debits an account by an amount
*
* @param {String} account_number the account number of the account to be debited
* @param {Number} amount the amount to be debited
*/
const debit = async (account_number, amount) => {
return await Accounts.findOneAndUpdate(
{ account_number },
{ $inc: { account_balance: -amount } },
{ new: true }
);
};
const transfer = async (req, res) => {
// debit the sender
// credit the recipient
// create the transaction
// return a response to throw an error
};
module.exports = { transfer };这是事务模式。
const { Schema, model, Types } = require("mongoose");
var TransactionSchema = new Schema(
{
description: {
type: String,
},
amount: {
type: Number,
require: true,
},
recipient: {
type: Types.ObjectId,
ref: "Account",
required: true,
},
sender: {
type: Types.ObjectId,
ref: "Account",
},
type: {
type: String,
enum: ["Debit", "Credit", "Reversal"],
required: true,
},
},
{ timestamps: true }
);
module.exports = model("Transaction", TransactionSchema);我如何处理传送逻辑?
发布于 2022-10-16 08:18:20
现在这取决于你在哪里,国家,以及银行系统是如何运作的。在我的国家,您总是需要在事务之前生成一个唯一的sessionID (即在名称验证过程中,在启动传输时生成另一个)。假设所有的预验证都已经完成,那么您当前的逻辑将需要额外的字段,这些字段在事务完成后会派上用场,但应该按原样工作。然而,以下各点至关重要:
https://stackoverflow.com/questions/71207726
复制相似问题