FIX: Transaction Bugs

This commit is contained in:
2025-05-03 15:47:33 +03:30
parent 35163f7ac7
commit 2e11c65b11
3 changed files with 17 additions and 1 deletions
@@ -23,6 +23,9 @@ exports.indexAllTransactions = catchAsync(async (req, res, next) => {
}) })
exports.createTransaction = 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) const transactions = await transactionService.createTransaction(req.body)
res.status(201).json({ res.status(201).json({
@@ -30,9 +30,14 @@ const transactionSchema = new mongoose.Schema(
type: ObjectID, type: ObjectID,
ref: 'User', ref: 'User',
}, },
userName: {
type: String,
required: true,
},
belongingId: { belongingId: {
type: ObjectID, type: ObjectID,
ref: 'Belonging', ref: 'Belonging',
required: true,
}, },
}, },
{ {
+9 -1
View File
@@ -47,6 +47,7 @@ const createTransaction = async (data) => {
description: data.description, description: data.description,
userId: data.userId, userId: data.userId,
belongingId: data.belongingId, belongingId: data.belongingId,
userName: data.userName,
}) })
} }
@@ -198,6 +199,7 @@ const indexAllTransactions = async () => {
const transactions = await Transaction.find({}, null, null) const transactions = await Transaction.find({}, null, null)
.skip((i - 1) * batchSize) .skip((i - 1) * batchSize)
.limit(batchSize) .limit(batchSize)
.populate('belongingId')
let documents = [] let documents = []
@@ -206,10 +208,13 @@ const indexAllTransactions = async () => {
_id: transaction._id, _id: transaction._id,
type: transaction.type, type: transaction.type,
status: transaction.status, status: transaction.status,
source: transaction.source, source: transaction.source || '',
value: transaction.value, value: transaction.value,
description: transaction.description, description: transaction.description,
createdAt: Math.floor(new Date(transaction.createdAt).getTime() / 1000), // Store as timestamp in seconds 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) documents.push(data)
@@ -233,6 +238,9 @@ const getTypeSenseSchema = () => {
{ name: 'value', type: 'int64', facet: false }, { name: 'value', type: 'int64', facet: false },
{ name: 'description', type: 'string', facet: false, infix: true }, { name: 'description', type: 'string', facet: false, infix: true },
{ name: 'createdAt', type: 'int64', facet: false }, { 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', default_sorting_field: 'createdAt',
} }