Files
Dwinzo-Backend-V0.0/src/api-server/controller/lines/zone-Services.ts

89 lines
3.8 KiB
TypeScript

import { Request, Response } from "express";
import zoneModel from "../../../shared/model/lines/zone-Model.ts";
export class zone {
static async setZone(req: Request, res: Response) {
try {
const { organization, userId, zoneData } = req.body
// console.log('req.body: ', req.body);
const zoneId = zoneData.zoneId
const points = zoneData.points
const zoneName = zoneData.zoneName
const layer = zoneData.layer
const viewPortCenter = zoneData.viewPortCenter
const viewPortposition = zoneData.viewPortposition
const findZoneId = await zoneModel(organization).findOne({ zoneId: zoneId })
if (findZoneId) {
const updateZone = await zoneModel(organization).findOneAndUpdate(
{ zoneId: zoneId }, { points: points, viewPortposition: viewPortposition, viewPortCenter: viewPortCenter }, { new: true }
).select("-_id -__v")
res.status(201).json({ message: 'zone updated', data: updateZone, organization: organization })
} else {
const zoneCreate = await zoneModel(organization).create({
zoneId, createBy: userId, zoneName: zoneName, points, layer, viewPortCenter, viewPortposition
})
const createdZone = await zoneModel(organization)
.findById(zoneCreate._id)
.select('-_id -__v')
.lean();
res.status(201).json({ message: 'zone created', data: createdZone, organization: organization })
}
} catch (error) {
console.log('error: ', error);
res.status(500).json({ message: 'Zone not found', error })
}
}
static async deleteZone(req: Request, res: Response) {
try {
const { organization, userId, zoneId } = req.body
const findZoneId = await zoneModel(organization).findOne({ zoneId: zoneId })
if (findZoneId) {
const deleteZone = await zoneModel(organization).findOneAndDelete(
{ zoneId: zoneId, createBy: userId }
).select("-_id -__v")
res.status(201).json({ message: 'zone deleted', data: deleteZone, organization: organization })
} else {
res.status(500).json({ message: 'Invalid zone ID' })
}
} catch (error) {
console.log('error: ', error);
res.status(500).json({ message: 'Zone not found', error })
}
}
static async getZones(req: Request, res: Response) {
try {
const { organization, userId } = req.params
const findZoneId = await zoneModel(organization)
.find()
.select("zoneId zoneName layer points viewPortCenter viewPortposition -_id");
if (!findZoneId) {
res.status(500).json({ message: 'Invalid zone' })
}
res.status(201).json({ data: findZoneId, organization: organization })
} catch (error) {
console.log('error: ', error);
res.status(500).json({ message: 'Zone not found', error })
}
}
static async ZoneData(req: Request, res: Response): Promise<any> {
try {
const organization = req.params.organization;
const zoneId = req.params.zoneId;
const findZone = await zoneModel(organization)
.findOne({ zoneId: zoneId })
// .select("zoneName");
console.log("findZone: ", findZone);
if (findZone) return res.status(200).json(findZone);
} catch (error: any) {
return res.status(500).send(error.message);
}
}
}