-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add site management endpoints #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1b5c560
fix: Updated docker-compose files for production and development envi…
ananyaa06 fea431a
feat(api): add secure site management and public sites endpoints
ananyaa06 6d7ebd9
feat(core): add mongoose schema for Site model
ananyaa06 8230e28
feat: add secure site management endpoints
ananyaa06 0992445
fix(routes): add leading slash to secure-site endpoints
ananyaa06 1c217d4
feat: add public sites API endpoint
ananyaa06 6e35be7
refactor: address PR review comments
ananyaa06 0af04c9
fix: address PR review comments
ananyaa06 5ce36c1
refactor: change sites to public-sites to not confuse with old endpoi…
ananyaa06 56f13c2
feat: add validation for latitude and longitude
ananyaa06 500adf2
docs: add comments to secure-site route handlers
ananyaa06 2f27281
refactor: replace api/public-sites with api/sites and make necessary …
ananyaa06 c78960d
feat: add testing coverage for secure-site.ts functions
ananyaa06 c99f4d9
refactor: run format
ananyaa06 f0361e7
feat: add test step to CI
ananyaa06 d737267
fix: make test action run before release
ananyaa06 488ea2e
refactor: remove old-sites
ananyaa06 c620d83
fix: put edit-sites behind secure
ananyaa06 2a99213
refactor: remove duplicate line
ananyaa06 ac44be1
refactor: remove empty line
ananyaa06 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import mongoose from 'mongoose'; | ||
| import { components } from '../types/schema'; | ||
|
|
||
| type ISite = components['schemas']['Site']; | ||
|
|
||
| interface SiteDoc extends mongoose.Document, ISite {} | ||
|
|
||
| interface SiteModelInterface extends mongoose.Model<SiteDoc> { | ||
| build(attr: ISite): SiteDoc; | ||
| } | ||
|
|
||
| const siteSchema = new mongoose.Schema( | ||
| { | ||
| name: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
| latitude: { | ||
| type: Number, | ||
| required: true, | ||
| }, | ||
| longitude: { | ||
| type: Number, | ||
| required: true, | ||
| }, | ||
| status: { | ||
| type: String, | ||
| enum: ['active', 'confirmed', 'in-conversation'], | ||
| required: true, | ||
| }, | ||
| address: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
| cell_id: { | ||
| type: [String], | ||
| required: true, | ||
| }, | ||
| color: { | ||
| type: String, | ||
| required: false, | ||
| }, | ||
| boundary: { | ||
| type: [[Number]], | ||
| validate: { | ||
| validator: function (val: number[][]) { | ||
| return val.every(pair => pair.length === 2); | ||
| }, | ||
| message: | ||
| 'Each boundary coordinate must be a pair of [latitude, longitude]', | ||
| }, | ||
| required: false, | ||
| }, | ||
| }, | ||
| { | ||
| versionKey: false, | ||
| }, | ||
| ); | ||
|
|
||
| siteSchema.statics.build = (attr: ISite) => { | ||
| return new Site(attr); | ||
| }; | ||
|
|
||
| const Site = mongoose.model<SiteDoc, SiteModelInterface>('Site', siteSchema); | ||
|
|
||
| export { Site, ISite, SiteDoc }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import express, { Request, Response } from 'express'; | ||
| import { Site } from '../models/site'; | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.get('/api/public-sites', async (req: Request, res: Response) => { | ||
ananyaa06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ananyaa06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| const sites = await Site.find(); | ||
|
|
||
| res.status(200).json({ | ||
| sites: sites, | ||
| }); | ||
| } catch (error) { | ||
| console.error('Error retrieving public sites:', error); | ||
| res.status(500).send('Internal server error'); | ||
| } | ||
| }); | ||
|
|
||
| export { router as publicSitesRouter }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import express, { Request, Response } from 'express'; | ||
| import { Site } from '../models/site'; | ||
| // import connectEnsureLogin from 'connect-ensure-login'; | ||
ananyaa06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { components } from '../types/schema'; | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| type SiteRequest = components['schemas']['Site']; | ||
|
|
||
| router.put( | ||
| '/api/secure-site', | ||
| // connectEnsureLogin.ensureLoggedIn('/api/failure'), | ||
| async (req: Request, res: Response) => { | ||
| try { | ||
| const siteData: SiteRequest = req.body; | ||
|
|
||
| if (!siteData.name) { | ||
| res.status(400).json({ error: 'Site name is required' }); | ||
| return; | ||
| } | ||
|
|
||
| const updatedSite = await Site.findOneAndUpdate( | ||
| { name: siteData.name }, | ||
| siteData, | ||
| { new: true, runValidators: true }, | ||
| ); | ||
|
|
||
| if (!updatedSite) { | ||
| res.status(404).json({ error: 'Site not found' }); | ||
| return; | ||
| } | ||
|
|
||
| res.status(200).json({ message: 'Site updated successfully' }); | ||
| } catch (error: any) { | ||
| res.status(500).json({ error: 'Internal server error' }); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| router.post( | ||
| '/api/secure-site', | ||
| // connectEnsureLogin.ensureLoggedIn('/api/failure'), | ||
| async (req: Request, res: Response) => { | ||
| try { | ||
| const siteData: SiteRequest = req.body; | ||
|
|
||
| const newSite = Site.build(siteData); | ||
| const savedSite = await newSite.save(); | ||
|
|
||
| res.status(201).json(savedSite); | ||
| } catch (error: any) { | ||
| if (error.name === 'ValidationError') { | ||
| res | ||
| .status(400) | ||
| .json({ error: 'Validation error', details: error.message }); | ||
| return; | ||
| } | ||
| res.status(500).json({ error: 'Internal server error' }); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| router.delete( | ||
| '/api/secure-site', | ||
| // connectEnsureLogin.ensureLoggedIn('/api/failure'), | ||
| async (req: Request, res: Response) => { | ||
| try { | ||
| const siteData: SiteRequest = req.body; | ||
|
|
||
| if (!siteData.name) { | ||
| res.status(400).json({ error: 'Site name is required' }); | ||
| return; | ||
| } | ||
|
|
||
| const deletedSite = await Site.findOneAndDelete({ name: siteData.name }); | ||
|
|
||
| if (!deletedSite) { | ||
| res.status(404).json({ error: 'Site not found' }); | ||
| return; | ||
| } | ||
|
|
||
| res | ||
| .status(200) | ||
| .json({ message: 'Site deleted successfully', site: deletedSite }); | ||
| } catch (error: any) { | ||
| res.status(500).json({ error: 'Internal server error' }); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| export { router as secureSitesRouter }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.