Skip to content

Commit

Permalink
Merge pull request #17 from TrackER-Corporation/feat/Api-Setup
Browse files Browse the repository at this point in the history
feat: update route
  • Loading branch information
LeleDallas committed May 2, 2023
2 parents 7f8c17d + 497bc63 commit 645213b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
25 changes: 19 additions & 6 deletions db/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,13 @@ export const updateBuildingResources = asyncHandler(async (req: any, res: any) =
})

export const getBuildingsById = asyncHandler(async (req: any, res: any) => {
let result = collections.buildings?.find({ userId: new ObjectId(req.params.id) }).toArray()
res.status()
if (!result) {
let result = await collections.buildings?.find({ userId: new ObjectId(req.params.id) }).toArray()
if (result?.length === 0) {
res.status(400)
// throw new Error('Goal not found')
}
res.status(200).json(result)

}else{
res.status(200).json(result)
}
});

export const getBuilding = asyncHandler(async (req: any, res: any) => {
Expand Down Expand Up @@ -162,3 +161,17 @@ export const deleteBuildingById = asyncHandler(async (req: any, res: any) => {
await building?.remove()
res.status(200).json({ id: req.params.id })
})

export const deleteBuildingByUserId = asyncHandler(async (req: any, res: any) => {
let myQuery = { userId: new ObjectId(req.params.id) };
const buildings = await collections?.buildings?.find(myQuery).toArray()
if (buildings?.length === 0 || buildings === undefined) {
res.status(400)
return
// throw new Error('Goal not found')
}
buildings?.forEach(async (building) => {
await collections?.buildings?.deleteOne(building)
});
res.status(200).json({buildings})
})
5 changes: 3 additions & 2 deletions db/route/route.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import express from 'express';
const router = express.Router();
import { registerBuilding, getBuildingsById, deleteBuildingById, getBuilding, getBuildings, updateBuilding, getBuildingsByOrganizationId, updateBuildingResources } from '../controller/controller';
import { registerBuilding, getBuildingsById, deleteBuildingById, getBuilding, getBuildings, updateBuilding, getBuildingsByOrganizationId, updateBuildingResources, deleteBuildingByUserId } from '../controller/controller';

// router.get('/:id', getBuildingsById)
router.get('/find/:id', getBuildingsById)
router.get('/:id', getBuilding)
router.get('/', getBuildings)
router.get('/organization/:id', getBuildingsByOrganizationId)
router.post('/register', registerBuilding)
router.put('/:id', updateBuilding)
router.put('/resources/:id', updateBuildingResources)
router.delete('/:id', deleteBuildingById)
router.delete('/user/:id', deleteBuildingByUserId)
export default router;
30 changes: 28 additions & 2 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { deleteBuildingById, getBuilding, getBuildings, getBuildingsById, getBuildingsByOrganizationId, registerBuilding, updateBuilding, updateBuildingResources } from "../db/controller/controller";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { deleteBuildingById, getBuilding, getBuildings, getBuildingsById, getBuildingsByOrganizationId, registerBuilding, updateBuilding, updateBuildingResources, deleteBuildingByUserId } from "../db/controller/controller";
import { ObjectId } from "mongodb";


Expand Down Expand Up @@ -284,4 +284,30 @@ describe('Building controller', () => {
await deleteBuildingById(req, res);
expect(res.status).toHaveBeenCalledWith(400);
})

it('should delete a building by User ID', async () => {
// Create a new building to be deleted
const req = {
params: {
_id: "test",
},
body: {
name: 'Test Building 2',
contact: 'test2@example.com',
userId: '999999999999',
address: '456 Main St, EAST',
type: 'Pool',
organizationId: '610a96a9f9d9b935a42a50a9',
sqft: 700,
lat: 47.7428,
long: -714.1030
}
};
const res = {
status: vi.fn().mockReturnThis(),
json: vi.fn()
};
await deleteBuildingByUserId(req, res);
expect(res.status).toHaveBeenCalledWith(400);
})
});

0 comments on commit 645213b

Please sign in to comment.