-
Notifications
You must be signed in to change notification settings - Fork 61
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 #2246 from zetkin/main
241009 Release
- Loading branch information
Showing
52 changed files
with
2,414 additions
and
458 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
93 changes: 93 additions & 0 deletions
93
src/app/beta/orgs/[orgId]/areas/[areaId]/tags/[tagId]/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,93 @@ | ||
import mongoose from 'mongoose'; | ||
import { notFound } from 'next/navigation'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { AreaModel } from 'features/areas/models'; | ||
import asOrgAuthorized from 'utils/api/asOrgAuthorized'; | ||
import { ZetkinTag } from 'utils/types/zetkin'; | ||
|
||
type RouteMeta = { | ||
params: { | ||
areaId: string; | ||
orgId: string; | ||
tagId: string; | ||
}; | ||
}; | ||
|
||
export async function PUT(request: NextRequest, { params }: RouteMeta) { | ||
return asOrgAuthorized( | ||
{ | ||
orgId: params.orgId, | ||
request: request, | ||
roles: ['admin'], | ||
}, | ||
async ({ orgId, apiClient }) => { | ||
await mongoose.connect(process.env.MONGODB_URL || ''); | ||
|
||
try { | ||
const tag = await apiClient.get<ZetkinTag>( | ||
`/api/orgs/${orgId}/people/tags/${params.tagId}` | ||
); | ||
|
||
const area = await AreaModel.findOne({ _id: params.areaId }); | ||
if (!area) { | ||
return notFound(); | ||
} | ||
|
||
let value: string | undefined = undefined; | ||
try { | ||
const payload = await request.json(); | ||
value = payload.value; | ||
} catch (err) { | ||
// Ignore this | ||
} | ||
|
||
const existingTags = area.tags || []; | ||
area.tags = [ | ||
// Other tags | ||
...existingTags.filter((item) => item.id != parseInt(params.tagId)), | ||
// New tag | ||
{ id: parseInt(params.tagId), value: value }, | ||
]; | ||
|
||
await area.save(); | ||
|
||
return NextResponse.json({ | ||
data: { | ||
...tag, | ||
value: value, | ||
}, | ||
}); | ||
} catch (err) { | ||
return notFound(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
export async function DELETE(request: NextRequest, { params }: RouteMeta) { | ||
return asOrgAuthorized( | ||
{ | ||
orgId: params.orgId, | ||
request: request, | ||
roles: ['admin'], | ||
}, | ||
async () => { | ||
await mongoose.connect(process.env.MONGODB_URL || ''); | ||
|
||
const area = await AreaModel.findOne({ _id: params.areaId }); | ||
if (!area) { | ||
return notFound(); | ||
} | ||
|
||
const existingTags = area.tags || []; | ||
area.tags = existingTags.filter( | ||
(item) => item.id != parseInt(params.tagId) | ||
); | ||
|
||
await area.save(); | ||
|
||
return new NextResponse(null, { status: 204 }); | ||
} | ||
); | ||
} |
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,49 @@ | ||
import mongoose from 'mongoose'; | ||
import { notFound } from 'next/navigation'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { AreaModel } from 'features/areas/models'; | ||
import asOrgAuthorized from 'utils/api/asOrgAuthorized'; | ||
import { ZetkinTag } from 'utils/types/zetkin'; | ||
|
||
type RouteMeta = { | ||
params: { | ||
areaId: string; | ||
orgId: string; | ||
}; | ||
}; | ||
|
||
export async function GET(request: NextRequest, { params }: RouteMeta) { | ||
return asOrgAuthorized( | ||
{ | ||
orgId: params.orgId, | ||
request: request, | ||
roles: ['admin'], | ||
}, | ||
async ({ orgId, apiClient }) => { | ||
await mongoose.connect(process.env.MONGODB_URL || ''); | ||
|
||
const tags: ZetkinTag[] = []; | ||
|
||
const area = await AreaModel.findOne({ _id: params.areaId }); | ||
if (area) { | ||
for await (const tagMeta of area.tags) { | ||
const tag = await apiClient.get<ZetkinTag>( | ||
`/api/orgs/${orgId}/people/tags/${tagMeta.id}` | ||
); | ||
|
||
tags.push({ | ||
...tag, | ||
value: tagMeta.value, | ||
}); | ||
} | ||
|
||
return NextResponse.json({ | ||
data: tags, | ||
}); | ||
} else { | ||
return notFound(); | ||
} | ||
} | ||
); | ||
} |
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.