zone creation
This commit is contained in:
13
src/api-server/Routes/zone-Routes.ts
Normal file
13
src/api-server/Routes/zone-Routes.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import express from 'express';
|
||||
|
||||
import { zone } from '../controller/lines/zone-Controller';
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
router.post('/setZone',zone.setZone)
|
||||
router.delete('/deleteZone',zone.deleteZone)
|
||||
router.get('/findZones/:organization',zone.getZones)
|
||||
|
||||
|
||||
|
||||
export default router;
|
||||
@@ -9,6 +9,8 @@ import flooritemRoutes from './Routes/flooritem-Routes'
|
||||
import WallitemRoutes from './Routes/wallItems-Routes'
|
||||
import userRoutes from './Routes/user-Routes'
|
||||
import shareRoutes from './Routes/share-Routes'
|
||||
import zoneRoutes from './Routes/zone-Routes'
|
||||
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
@@ -28,4 +30,5 @@ app.use('/api/v1', flooritemRoutes);
|
||||
app.use('/api/v1', WallitemRoutes);
|
||||
app.use('/api/v1', userRoutes);
|
||||
app.use('/api/v1', shareRoutes);
|
||||
app.use('/api/v2', zoneRoutes);
|
||||
export default app;
|
||||
|
||||
71
src/api-server/controller/lines/zone-Controller.ts
Normal file
71
src/api-server/controller/lines/zone-Controller.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { Request, Response } from "express";
|
||||
import zoneModel from "../../../shared/model/lines/zone-Model";
|
||||
export class zone {
|
||||
static async setZone(req: Request, res: Response) {
|
||||
try {
|
||||
const {organization,userId,zoneData}=req.body
|
||||
const zoneId =zoneData.zoneId
|
||||
const points =zoneData.points
|
||||
const zoneName =zoneData.zoneName
|
||||
const layer =zoneData.layer
|
||||
const findZoneId= await zoneModel(organization).findOne({zoneId:zoneId})
|
||||
if (findZoneId) {
|
||||
const updateZone= await zoneModel(organization).findOneAndUpdate(
|
||||
{zoneId:zoneId},{points:points},{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
|
||||
})
|
||||
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 -_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 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ try {
|
||||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
||||
const organization = process.env.ORGANIZATION_NAME || 'defaultOrganization'; // Replace with your logic
|
||||
|
||||
mongoAdminCreation()
|
||||
// mongoAdminCreation()
|
||||
if (!organization) {
|
||||
throw new Error('ORGANIZATION_NAME is not defined in the environment');
|
||||
}
|
||||
|
||||
26
src/shared/model/lines/zone-Model.ts
Normal file
26
src/shared/model/lines/zone-Model.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import mongoose, { Document, ObjectId, Schema } from "mongoose";
|
||||
import MainModel from "../../connect/mongoose";
|
||||
export interface zoneSchema extends Document {
|
||||
zoneId: string;
|
||||
zoneName: string
|
||||
createBy: mongoose.Types.ObjectId
|
||||
points: []
|
||||
layer: Number
|
||||
|
||||
}
|
||||
|
||||
// Define the Mongoose Schema
|
||||
const zoneSchema: Schema = new Schema({
|
||||
zoneId: { type: String },
|
||||
zoneName: { type: String },
|
||||
createBy: { type: Schema.Types.ObjectId, ref: "Users", },
|
||||
points: { type: Array },
|
||||
layer: { type: Number, required: true },
|
||||
});
|
||||
|
||||
|
||||
// export default zoneModel;
|
||||
const zoneModel = (db: string) => {
|
||||
return MainModel(db, "zones", zoneSchema, "zones")
|
||||
};
|
||||
export default zoneModel;
|
||||
50
src/socket-server/services/lines/zone-controller.ts
Normal file
50
src/socket-server/services/lines/zone-controller.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import zoneModel from "../../../shared/model/lines/zone-Model";
|
||||
|
||||
export const setZone = async (data:any)=>{
|
||||
try {
|
||||
const {organization,userId,zoneData}=data
|
||||
const zoneId =zoneData.zoneId
|
||||
const points =zoneData.points
|
||||
const zoneName =zoneData.zoneName
|
||||
const layer =zoneData.layer
|
||||
const findZoneId= await zoneModel(organization).findOne({zoneId:zoneId})
|
||||
if (findZoneId) {
|
||||
const updateZone= await zoneModel(organization).findOneAndUpdate(
|
||||
{zoneId:zoneId},{points:points},{new:true}
|
||||
).select("-_id -__v")
|
||||
return { success: true, message: 'zone updated', data: updateZone,organization:organization}
|
||||
} else {
|
||||
const zoneCreate = await zoneModel(organization).create({
|
||||
zoneId,createBy:userId,zoneName:zoneName,points,layer
|
||||
})
|
||||
const createdZone = await zoneModel(organization)
|
||||
.findById(zoneCreate._id)
|
||||
.select('-_id -__v')
|
||||
.lean();
|
||||
return { success: true, message: 'zone created', data: createdZone,organization:organization}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('error: ', error);
|
||||
return { success: false, message: 'Zone not found',error }
|
||||
}
|
||||
}
|
||||
|
||||
export const deleteZone = async (data:any)=>{
|
||||
try {
|
||||
const {organization,userId,zoneId}=data
|
||||
|
||||
const findZoneId= await zoneModel(organization).findOne({zoneId:zoneId})
|
||||
if (findZoneId) {
|
||||
const deleteZone= await zoneModel(organization).findOneAndDelete(
|
||||
{zoneId:zoneId,createBy:userId}
|
||||
).select("-_id -__v")
|
||||
return { success: true, message: 'zone deleted', data: deleteZone,organization:organization}
|
||||
} else {
|
||||
|
||||
return { success: true, message: 'Invalid zone ID',organization:organization}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('error: ', error);
|
||||
return { success: false, message: 'Zone not found',error }
|
||||
}
|
||||
}
|
||||
@@ -42,4 +42,9 @@ export const EVENTS = {
|
||||
deletePointResponse:"Line:response:delete:point",
|
||||
deleteLineLayer:"v1:Line:delete:layer",
|
||||
deleteLineLayerResponse:"Line:response:delete:layer",
|
||||
//zone
|
||||
setZone:"v2:zone:set",
|
||||
zoneUpdateRespones:"zone:response:updates",
|
||||
deleteZone:"v2:zone:delete",
|
||||
ZoneDeleteRespones:"zone:response:delete",
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { deleteFloorItems, setFloorItems } from '../services/assets/flooritem-Co
|
||||
import { deleteWallItems, setWallItems } from '../services/assets/wallitem-Controller';
|
||||
import { deleteLineItems, deleteLinPoiteItems, updateLineItems ,createLineItems, deleteLayer} from '../services/lines/line-Controller';
|
||||
import { activeUserOffline, activeUsers } from '../services/users/user-controller';
|
||||
import { deleteZone, setZone } from '../services/lines/zone-controller';
|
||||
|
||||
|
||||
|
||||
@@ -385,6 +386,44 @@ const userStatus =async (event: string, socket: Socket, data: any,io:any) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
const zoneHandleEvent =async (event: string, socket: Socket, data: any,io:any)=>{
|
||||
switch (event) {
|
||||
case EVENTS.setZone:{
|
||||
const result=await setZone(data)
|
||||
if (result?.success) {
|
||||
io.emit(EVENTS.zoneUpdateRespones, {
|
||||
success: true || false,
|
||||
message: result.message,
|
||||
data: result.data,
|
||||
error: result.error,
|
||||
socketId: socket.id,
|
||||
organization:result.organization
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
break}
|
||||
case EVENTS.deleteZone:{
|
||||
const result=await deleteZone(data)
|
||||
if (result?.success) {
|
||||
io.emit(EVENTS.ZoneDeleteRespones, {
|
||||
success: true || false,
|
||||
message: result.message,
|
||||
data: result.data,
|
||||
error: result.error,
|
||||
socketId: socket.id,
|
||||
organization:result.organization
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
break}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
export const initSocketServer = (httpServer: any) => {
|
||||
const io = new Server(httpServer, {
|
||||
cors: {
|
||||
@@ -406,6 +445,7 @@ userStatus(EVENTS.connection, socket, socket.handshake.auth,io);
|
||||
floorItemsHandleEvent(event, socket, data,io);
|
||||
wallItemsHandleEvent(event, socket, data,io);
|
||||
lineHandleEvent(event, socket, data,io);
|
||||
zoneHandleEvent(event, socket, data,io);
|
||||
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user