Files
mbmanager-api/components/belonging/belongingModel.js
T
kavehhn 4fc323cd16 ADD: API_URL to env
ADD: DocumentUrls and mainImageUrls field in belonging model
FIX: Indexing bugs for belonging
ADD: getImage endpoint to get private images
ADD: PrivateUrl field as virtual
FIX: Minor bug in bucketDeleteFile service
ADD: Word type to multer file checker
2025-04-27 16:17:17 +03:30

78 lines
2.1 KiB
JavaScript

const mongoose = require('mongoose')
const ObjectID = mongoose.Schema.Types.ObjectId
const AutoIncrement = require('mongoose-sequence')(mongoose)
const { getMultipleSignedUrls, getSignedUrl } = require('../file/fileService')
const belongingSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
unique: true,
},
belongingCode: {
type: Number,
unique: true,
},
category: {
type: ObjectID,
ref: 'Category',
},
unit: {
type: String,
required: true,
},
description: String,
relatedProjects: [String],
location: {
type: {
type: String,
enum: ['Point'], // GeoJSON type
required: false,
},
coordinates: {
type: [Number], // Array of numbers: [longitude, latitude]
required: false,
},
},
documentUrls: [String],
mainImageUrls: [String],
tags: [String],
isActive: {
type: Boolean,
default: true,
select: false,
},
features: [
{
definition: {
type: ObjectID,
ref: 'Feature',
required: true,
},
value: mongoose.Schema.Types.Mixed,
},
],
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
timestamps: true,
}
)
belongingSchema.plugin(AutoIncrement, { inc_field: 'belongingCode' })
belongingSchema.methods.getDocumentsSignedUrls = function () {
return getMultipleSignedUrls(this.documentUrls, 'belonging-documents')
}
belongingSchema.methods.getMainImagedSignedUrl = function () {
return getMultipleSignedUrls(this.mainImageUrls, 'belonging-images')
}
belongingSchema.index({ location: '2dsphere' }) // Enable geospatial queries
const Belonging = mongoose.model('Belonging', belongingSchema)
module.exports = Belonging