44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import Model from "./model.js";
|
|
import { coordinatesToGeoJson } from "../../helpers/geoJson.helper.js";
|
|
import dayjs from "dayjs";
|
|
// Create
|
|
export const createLocation = async (data) => {
|
|
const geoJson = coordinatesToGeoJson(data.coordinates);
|
|
return await Model.create({
|
|
...data,
|
|
coordinates: geoJson,
|
|
createdOn: data?.recorded_at,
|
|
});
|
|
};
|
|
|
|
// Read (Get by ID)
|
|
export const getLocationById = async (id) => {
|
|
return await Model.findById(id);
|
|
};
|
|
|
|
// Read (Get by Query)
|
|
export const getLocationsByQuery = async (query) => {
|
|
console.log("QUERY", query);
|
|
return await Model.find(query).sort({ createdOn: -1 });
|
|
};
|
|
|
|
// Read (Get by Radius)
|
|
export const getLocationsByRadius = async (radius) => {
|
|
return await Model.find(query);
|
|
};
|
|
|
|
// Read (Get all)
|
|
export const getAllLocations = async () => {
|
|
return await Model.find();
|
|
};
|
|
|
|
// Update
|
|
export const updateLocation = async (id, data) => {
|
|
return await Model.findByIdAndUpdate(id, data, { new: true });
|
|
};
|
|
|
|
// Aggregate
|
|
export const aggregateLocation = async (pipeline) => {
|
|
return await Model.aggregate(pipeline);
|
|
};
|