diff --git a/components/transaction/transactionController.js b/components/transaction/transactionController.js index a4199f6..43d7d8c 100644 --- a/components/transaction/transactionController.js +++ b/components/transaction/transactionController.js @@ -23,6 +23,9 @@ exports.indexAllTransactions = catchAsync(async (req, res, next) => { }) exports.createTransaction = catchAsync(async (req, res, next) => { + req.body.userId = req.user._id + req.body.userName = req.user.name + console.log(req.body) const transactions = await transactionService.createTransaction(req.body) res.status(201).json({ diff --git a/components/transaction/transactionModel.js b/components/transaction/transactionModel.js index e3bbd36..33a0f7f 100644 --- a/components/transaction/transactionModel.js +++ b/components/transaction/transactionModel.js @@ -30,9 +30,14 @@ const transactionSchema = new mongoose.Schema( type: ObjectID, ref: 'User', }, + userName: { + type: String, + required: true, + }, belongingId: { type: ObjectID, ref: 'Belonging', + required: true, }, }, { diff --git a/components/transaction/transactionService.js b/components/transaction/transactionService.js index 7a1ef35..ec5eae6 100644 --- a/components/transaction/transactionService.js +++ b/components/transaction/transactionService.js @@ -47,6 +47,7 @@ const createTransaction = async (data) => { description: data.description, userId: data.userId, belongingId: data.belongingId, + userName: data.userName, }) } @@ -198,6 +199,7 @@ const indexAllTransactions = async () => { const transactions = await Transaction.find({}, null, null) .skip((i - 1) * batchSize) .limit(batchSize) + .populate('belongingId') let documents = [] @@ -206,10 +208,13 @@ const indexAllTransactions = async () => { _id: transaction._id, type: transaction.type, status: transaction.status, - source: transaction.source, + source: transaction.source || '', value: transaction.value, description: transaction.description, createdAt: Math.floor(new Date(transaction.createdAt).getTime() / 1000), // Store as timestamp in seconds + belongingName: transaction.belongingId.name, + belongingId: transaction.belongingId._id, + userName: transaction.userName || '', } documents.push(data) @@ -233,6 +238,9 @@ const getTypeSenseSchema = () => { { name: 'value', type: 'int64', facet: false }, { name: 'description', type: 'string', facet: false, infix: true }, { name: 'createdAt', type: 'int64', facet: false }, + { name: 'belongingName', type: 'string', facet: false, infix: true }, + { name: 'belongingId', type: 'string', facet: false, infix: true }, + { name: 'userName', type: 'string', facet: false, infix: true }, ], default_sorting_field: 'createdAt', }