-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2357 from zetkin/undocumented/logging-to-place
Logging visits to places/areas instead of households
- Loading branch information
Showing
36 changed files
with
1,646 additions
and
386 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
97 changes: 97 additions & 0 deletions
97
src/app/beta/orgs/[orgId]/canvassassignments/[canvassAssId]/visits/route.ts
This file contains 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,97 @@ | ||
import mongoose from 'mongoose'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { | ||
CanvassAssignmentModel, | ||
PlaceVisitModel, | ||
} from 'features/canvassAssignments/models'; | ||
import asCanvasserAuthorized from 'features/canvassAssignments/utils/asCanvasserAuthorized'; | ||
|
||
type RouteMeta = { | ||
params: { | ||
canvassAssId: string; | ||
orgId: string; | ||
}; | ||
}; | ||
|
||
export async function GET(request: NextRequest, { params }: RouteMeta) { | ||
const canvassAssId = params.canvassAssId; | ||
return asCanvasserAuthorized( | ||
{ | ||
orgId: params.orgId, | ||
request: request, | ||
}, | ||
async ({ orgId }) => { | ||
await mongoose.connect(process.env.MONGODB_URL || ''); | ||
|
||
const assignmentModel = await CanvassAssignmentModel.find({ | ||
_id: canvassAssId, | ||
orgId: orgId, | ||
}); | ||
|
||
if (!assignmentModel) { | ||
return NextResponse.json({ error: {} }, { status: 404 }); | ||
} | ||
|
||
const visitModels = await PlaceVisitModel.find({ | ||
canvassAssId: canvassAssId, | ||
}); | ||
|
||
return NextResponse.json({ | ||
data: visitModels.map((model) => ({ | ||
canvassAssId: model.canvassAssId, | ||
id: model._id.toString(), | ||
personId: model.personId, | ||
placeId: model.placeId, | ||
responses: model.responses, | ||
timestamp: model.timestamp, | ||
})), | ||
}); | ||
} | ||
); | ||
} | ||
|
||
export async function POST(request: NextRequest, { params }: RouteMeta) { | ||
const canvassAssId = params.canvassAssId; | ||
return asCanvasserAuthorized( | ||
{ | ||
orgId: params.orgId, | ||
request: request, | ||
}, | ||
async ({ orgId, personId }) => { | ||
await mongoose.connect(process.env.MONGODB_URL || ''); | ||
|
||
const assignmentModel = await CanvassAssignmentModel.find({ | ||
_id: canvassAssId, | ||
orgId: orgId, | ||
}); | ||
|
||
if (!assignmentModel) { | ||
return NextResponse.json({ error: {} }, { status: 404 }); | ||
} | ||
|
||
const payload = await request.json(); | ||
|
||
const visit = new PlaceVisitModel({ | ||
canvassAssId, | ||
personId: personId, | ||
placeId: payload.placeId, | ||
responses: payload.responses, | ||
timestamp: new Date().toISOString(), | ||
}); | ||
|
||
await visit.save(); | ||
|
||
return NextResponse.json({ | ||
data: { | ||
canvassAssId: visit.canvassAssId, | ||
id: visit._id.toString(), | ||
personId: visit.personId, | ||
placeId: visit.placeId, | ||
responses: visit.responses, | ||
timestamp: visit.timestamp, | ||
}, | ||
}); | ||
} | ||
); | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.