FIX: Conflict
This commit is contained in:
@@ -1,6 +1,25 @@
|
||||
const catchAsync = require('../../globalErrorHandler/catchAsync')
|
||||
const belongingService = require('./belongingService')
|
||||
|
||||
exports.searchBelongings = catchAsync(async (req, res, next) => {
|
||||
const {facets, belongings, totalCount, pagesCount} = await belongingService.searchBelongings()
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
facets, belongings, totalCount, pagesCount
|
||||
})
|
||||
})
|
||||
|
||||
exports.indexAllBelongings = catchAsync(async (req, res, next) => {
|
||||
const { indexedCount } = await belongingService.indexAllBelongings()
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
indexedCount
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
exports.getAllBelongings = catchAsync(async (req, res, next) => {
|
||||
const belongings = await belongingService.getAllBelongings()
|
||||
|
||||
|
||||
@@ -9,15 +9,11 @@ const belongingSchema = new mongoose.Schema(
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
belongingCategory: {
|
||||
category: {
|
||||
type: ObjectID,
|
||||
ref: 'Category',
|
||||
},
|
||||
projectCode: {
|
||||
type: String,
|
||||
required: true,
|
||||
default: false,
|
||||
},
|
||||
description: String,
|
||||
relatedProjects: [String],
|
||||
location: {
|
||||
type: {
|
||||
|
||||
@@ -11,6 +11,8 @@ router.get('/admin/get-all', perm.requires('getallbelongings'), belongingControl
|
||||
|
||||
router.post('/admin/create', perm.requires('createabelonging'), belongingController.createBelonging)
|
||||
|
||||
router.post('/admin/index-all', perm.requires('indexbelongings', belongingController.indexAllBelongings))
|
||||
|
||||
router.get('/admin/get-one/:belongingId', perm.requires('getabelonging'), belongingController.getBelonging)
|
||||
|
||||
router.patch('/admin/update/:belongingId', perm.requires('updateabelonging'), belongingController.updateBelonging)
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
const Belonging = require('./belongingModel')
|
||||
const AppError = require('../../globalErrorHandler/AppError')
|
||||
const { client } = require('../../setup/typesense')
|
||||
const TsApiFeatures = require("../../utils/TsApiFeatures");
|
||||
|
||||
module.exports.getAllBelongings = async () => {
|
||||
const getAllBelongings = async () => {
|
||||
return Belonging.find({}, null, null)
|
||||
}
|
||||
|
||||
module.exports.createBelonging = async (data) => {
|
||||
const createBelonging = async (data) => {
|
||||
return Belonging.create(data, null)
|
||||
}
|
||||
|
||||
module.exports.getBelonging = async (belongingId) => {
|
||||
const getBelonging = async (belongingId) => {
|
||||
const belonging = await Belonging.findById(belongingId, null, null)
|
||||
|
||||
if (!belonging) {
|
||||
@@ -19,7 +21,7 @@ module.exports.getBelonging = async (belongingId) => {
|
||||
return belonging
|
||||
}
|
||||
|
||||
module.exports.updateBelonging = async (belongingId, data) => {
|
||||
const updateBelonging = async (belongingId, data) => {
|
||||
const belonging = await Belonging.findOneAndUpdate({ _id: belongingId }, data, { new: true })
|
||||
|
||||
if (!belonging) {
|
||||
@@ -29,7 +31,7 @@ module.exports.updateBelonging = async (belongingId, data) => {
|
||||
return belonging
|
||||
}
|
||||
|
||||
module.exports.deleteBelonging = async (belongingId) => {
|
||||
const deleteBelonging = async (belongingId) => {
|
||||
const belonging = await Belonging.findByIdAndUpdate(belongingId, { isActive: false }, { new: true })
|
||||
|
||||
if (!belonging) {
|
||||
@@ -38,3 +40,84 @@ module.exports.deleteBelonging = async (belongingId) => {
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const ensureCollectionExists = async () => {
|
||||
const collections = await client.collections().retrieve()
|
||||
const collectionExists = collections.some((collection) => collection.name === 'belongings')
|
||||
|
||||
if (collectionExists) {
|
||||
await client.collections('belongings').delete()
|
||||
}
|
||||
|
||||
const schema = getTypeSenseSchema()
|
||||
|
||||
await client.collections().create(schema)
|
||||
console.log('Collection "belongings" created successfully')
|
||||
}
|
||||
|
||||
const searchBelongings = async (inputQuery) => {
|
||||
const tsFeatures = new TsApiFeatures(client, 'belongings', inputQuery).filter().facet().sort().limitFields().paginate().search()
|
||||
|
||||
let {facets, items, totalCount, pagesCount} = await tsFeatures.execute()
|
||||
|
||||
return {facets, belongings: items, totalCount, pagesCount}
|
||||
}
|
||||
const indexAllBelongings = async () => {
|
||||
const batchSize = 20
|
||||
const count = await Belonging.countDocuments({}, null)
|
||||
const rounds = Math.ceil(count / batchSize)
|
||||
|
||||
await ensureCollectionExists()
|
||||
|
||||
for (let i = 1; i <= rounds; i++) {
|
||||
console.log(`Indexing ${batchSize} belongings, Round ${i}`)
|
||||
|
||||
const belongings = await Belonging.find({}, null, null)
|
||||
.populate('category')
|
||||
.skip((i - 1) * batchSize)
|
||||
.limit(batchSize)
|
||||
|
||||
let documents = []
|
||||
|
||||
for (const belonging of belongings) {
|
||||
|
||||
const data = {
|
||||
_id: belonging._id,
|
||||
isActive: belonging.isActive,
|
||||
belongingCode: belonging.belongingCode,
|
||||
category: belonging.category.map( x=> x.englishName),
|
||||
nativeCategory: belonging.category.map( x=> x.nativeName),
|
||||
documents: belonging.documents,
|
||||
description: belonging.description,
|
||||
coordinates: belonging.location.coordinates
|
||||
}
|
||||
|
||||
documents.push(data)
|
||||
}
|
||||
|
||||
await client.collections('belonging').documents().import(documents, {action: 'emplace'})
|
||||
}
|
||||
|
||||
return {totalCount: count}
|
||||
}
|
||||
|
||||
|
||||
const getTypeSenseSchema = () => {
|
||||
return {
|
||||
name: 'belongings',
|
||||
enable_nested_fields: true,
|
||||
fields: [
|
||||
{ name: '_id', type: 'string', facet: false, infix: false },
|
||||
{ name: 'isActive', type: 'bool', facet: false, infix: true },
|
||||
{ name: 'belongingCode', type: 'string', facet: true },
|
||||
{ name: 'category', type: 'string[]', facet: true },
|
||||
{ name: 'nativeCategory', type: 'string[]', facet: false },
|
||||
{ name: 'documents', type: 'string[]', facet: false },
|
||||
{ name: 'description', type: 'string', facet: false },
|
||||
{ name: 'coordinates', type: 'geopoint' },
|
||||
],
|
||||
default_sorting_field: 'createdAt',
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging, searchBelongings}
|
||||
Reference in New Issue
Block a user