Files
mbmanager-api/components/belonging/belongingModel.js
T
kavehhn 2d87cc4834 ADD: SignedUrls for documents, mainImage and images
ADD: Bucket and Upload System
ADD: New errors
ADD: Virtuals for categories
ADD: Features system
ADD: File management system
2025-01-04 22:06:43 +03:30

69 lines
1.9 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(
{
belongingCode: {
type: String,
required: true,
unique: true,
},
category: {
type: ObjectID,
ref: 'Category',
},
description: String,
relatedProjects: [String],
location: {
type: {
type: String,
enum: ['Point'], // GeoJSON type
required: true,
},
coordinates: {
type: [Number], // Array of numbers: [longitude, latitude]
required: true,
},
},
documentNames: [String],
imageNames: [String],
mainImageNames: [String],
numId: {
type: Number,
unique: true,
},
tags: [String],
isActive: {
type: Boolean,
default: true,
select: false,
},
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
timestamps: true,
}
)
belongingSchema.plugin(AutoIncrement, { inc_field: 'numId' })
belongingSchema.methods.getDocumentsSignedUrls = async function () {
return getMultipleSignedUrls(this.documentNames, 'belonging-images')
}
belongingSchema.methods.getImagesSignedUrls = async function () {
return getMultipleSignedUrls(this.imageNames, 'belonging-images')
}
belongingSchema.methods.getMainImagedSignedUrl = async function () {
return getMultipleSignedUrls(this.mainImageNames, 'belonging-images')
}
belongingSchema.index({ location: '2dsphere' }) // Enable geospatial queries
const Belonging = mongoose.model('Belonging', belongingSchema)
module.exports = Belonging