REFACTOR: Belonging → Property system migration + utilities update

Property system:
- Rename belonging → property throughout (model, controller, routes, service)
- Add property sub-models: buildingModel, landModel, belongingModel
- Add property lifecycle events: createProperty, deleteProperty
- Add propertyStatus component (model, controller, routes, service, seeder)
- Add commodity and commodityTransaction model stubs

Infrastructure:
- Update transactionModel and transactionService
- Update events/index.js for new property events
- Update seeder (seederDBConnection, seederRunner)
- Update typesense client configuration
- Update upload utilities (bucket, minio)
- Update featureModel and featureSeeder
- Update categorySeeder
- Update TsApiFeatures utility
- Remove docker-compose.yml (moved to root)
- Remove legacy belonging component files
This commit is contained in:
2026-08-11 20:35:47 +03:30
parent a9432b96c0
commit ca911b97a0
35 changed files with 1504 additions and 1223 deletions
+4 -8
View File
@@ -34,10 +34,9 @@ const transactionSchema = new mongoose.Schema(
type: String,
required: true,
},
belongingId: {
propertyId: {
type: ObjectID,
ref: 'Belonging',
required: true,
ref: 'Property',
},
},
{
@@ -47,14 +46,11 @@ const transactionSchema = new mongoose.Schema(
}
)
// Add individual indexes for frequently queried fields
transactionSchema.index({ type: 1 })
transactionSchema.index({ status: 1 })
transactionSchema.index({ value: 1 })
// Add compound indexes for common query patterns in aggregation
transactionSchema.index({ belongingId: 1, status: 1 })
transactionSchema.index({ belongingId: 1, type: 1, status: 1 })
transactionSchema.index({ propertyId: 1, status: 1 })
transactionSchema.index({ propertyId: 1, type: 1, status: 1 })
const Transaction = mongoose.model('Transaction', transactionSchema)
module.exports = Transaction
+45 -43
View File
@@ -1,5 +1,5 @@
const Transaction = require('./transactionModel')
const Belonging = require('../belonging/belongingModel')
const Property = require('../property/propertyModel')
const AppError = require('../../globalErrorHandler/AppError')
const mongoose = require('mongoose')
const ObjectID = mongoose.Schema.Types.ObjectId
@@ -17,17 +17,16 @@ const filterObj = (obj, ...allowedFields) => {
const createTransaction = async (data) => {
if (data.type == 'withdraw') {
// Aggregate transactions to calculate inventory
const result = await Transaction.aggregate([
{
$match: {
belongingId: ObjectID,
propertyId: ObjectID,
status: 'finished',
},
},
{
$group: {
_id: '$belongingId',
_id: '$propertyId',
totalInventory: {
$sum: '$value',
},
@@ -35,7 +34,7 @@ const createTransaction = async (data) => {
},
])
if (result[0].totalInventory <= 0) throw new AppError('Invalid Transaction', 404, {})
if (result[0]?.totalInventory <= 0) throw new AppError('Invalid Transaction', 404, {})
}
return Transaction.create({
@@ -46,7 +45,7 @@ const createTransaction = async (data) => {
date: data.Date,
description: data.description,
userId: data.userId,
belongingId: data.belongingId,
propertyId: data.propertyId || data.belongingId,
userName: data.userName,
})
}
@@ -55,28 +54,26 @@ const getUserTransaction = async (userId) => {
return Transaction.find({ userId }, null, null)
}
const getTransactionByBelongingId = async (belongingId) => {
return Transaction.find({ belongingId }, null, null)
const getTransactionByBelongingId = async (propertyId) => {
return Transaction.find({ propertyId }, null, null)
}
const getBelongingInventory = async (belongingId) => {
const belonging = await Belonging.findById(belongingId, null, null)
const getBelongingInventory = async (propertyId) => {
const property = await Property.findById(propertyId, null, null)
if (!belonging) {
throw new AppError('NoBelongingFound', 404, {})
if (!property) {
throw new AppError('NoPropertyFound', 404, {})
}
// Simplified aggregation to calculate inventory metrics
const result = await Transaction.aggregate([
{
$match: {
belongingId: belonging._id,
propertyId: property._id,
$or: [{ status: 'finished' }, { status: 'pending' }],
},
},
{
$facet: {
// Calculate total inventory from finished transactions
finishedInventory: [
{ $match: { status: 'finished' } },
{
@@ -86,7 +83,6 @@ const getBelongingInventory = async (belongingId) => {
},
},
],
// Calculate pending deposits
pendingDepositsInventory: [
{
$match: {
@@ -101,7 +97,6 @@ const getBelongingInventory = async (belongingId) => {
},
},
],
// Calculate pending withdrawals
pendingWithdrawsInventory: [
{
$match: {
@@ -119,7 +114,6 @@ const getBelongingInventory = async (belongingId) => {
},
},
{
// Restructure the output to get only the values
$project: {
finishedInventory: { $ifNull: [{ $arrayElemAt: ['$finishedInventory.total', 0] }, 0] },
pendingDepositsInventory: { $ifNull: [{ $arrayElemAt: ['$pendingDepositsInventory.total', 0] }, 0] },
@@ -152,21 +146,27 @@ const deleteTransaction = async (transactionId) => {
return null
}
const ensureCollectionExists = async () => {
const collections = await client.collections().retrieve()
const collectionExists = collections.some((collection) => collection.name === 'transactions')
const ensureCollectionExists = async (forceRecreate = false) => {
try {
const collections = await client.collections().retrieve()
const collectionExists = collections.some((collection) => collection.name === 'transactions')
if (collectionExists) {
await client.collections('transactions').delete()
if (collectionExists && forceRecreate) {
await client.collections('transactions').delete()
}
if (!collectionExists || forceRecreate) {
const schema = getTypeSenseSchema()
await client.collections().create(schema)
console.log('Collection "transactions" created successfully')
}
} catch (err) {
console.error('Error ensuring transactions collection exists:', err)
}
const schema = getTypeSenseSchema()
await client.collections().create(schema)
console.log('Collection "transactions" created successfully')
}
const searchTransactions = async (inputQuery) => {
await ensureCollectionExists(false)
inputQuery = updateInputQuery(inputQuery, {})
const tsFeatures = new TsApiFeatures(client, 'transactions', inputQuery).filter().facet().sort().limitFields().paginate().search()
@@ -176,7 +176,7 @@ const searchTransactions = async (inputQuery) => {
return { facets, transactions: items, totalCount, pagesCount }
}
const updateInputQuery = (inputQuery, extraQueries, features) => {
const updateInputQuery = (inputQuery, extraQueries) => {
for (const [key, value] of Object.entries(extraQueries)) {
inputQuery[key] = value
}
@@ -189,38 +189,40 @@ const updateInputQuery = (inputQuery, extraQueries, features) => {
const indexAllTransactions = async () => {
const batchSize = 20
const count = await Transaction.countDocuments({}, null)
const rounds = Math.ceil(count / batchSize)
const rounds = Math.max(1, Math.ceil(count / batchSize))
await ensureCollectionExists()
await ensureCollectionExists(true)
for (let i = 1; i <= rounds; i++) {
console.log(`Indexing ${batchSize} transactions, Round ${i}`)
const transactions = await Transaction.find({}, null, null)
.skip((i - 1) * batchSize)
.limit(batchSize)
.populate('belongingId')
.populate('propertyId')
if (transactions.length === 0) break
let documents = []
for (const transaction of transactions) {
const data = {
_id: transaction._id,
type: transaction.type,
status: transaction.status,
_id: transaction._id.toString(),
type: transaction.type || '',
status: transaction.status || '',
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,
value: transaction.value || 0,
description: transaction.description || '',
createdAt: Math.floor(new Date(transaction.createdAt || Date.now()).getTime() / 1000),
belongingName: transaction.propertyId?.name || '',
belongingId: transaction.propertyId?._id ? transaction.propertyId._id.toString() : '',
userName: transaction.userName || '',
}
documents.push(data)
}
await client.collections('transactions').documents().import(documents, { action: 'emplace' })
if (documents.length > 0) {
await client.collections('transactions').documents().import(documents, { action: 'emplace' })
}
}
return { totalCount: count }