Skip to content

Commit

Permalink
Merge pull request #2246 from zetkin/main
Browse files Browse the repository at this point in the history
241009 Release
  • Loading branch information
richardolsson authored Oct 9, 2024
2 parents 76259f4 + 811587d commit 4392ce6
Showing 52 changed files with 2,414 additions and 458 deletions.
36 changes: 34 additions & 2 deletions src/app/beta/orgs/[orgId]/areas/[areaId]/route.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { AreaModel } from 'features/areas/models';
import asOrgAuthorized from 'utils/api/asOrgAuthorized';
import { ZetkinArea } from 'features/areas/types';
import { ZetkinTag } from 'utils/types/zetkin';

type RouteMeta = {
params: {
@@ -19,22 +20,37 @@ export async function GET(request: NextRequest, { params }: RouteMeta) {
request: request,
roles: ['admin', 'organizer'],
},
async ({ orgId }) => {
async ({ apiClient, orgId }) => {
await mongoose.connect(process.env.MONGODB_URL || '');

const areaModel = await AreaModel.findOne({ _id: params.areaId, orgId });
const allTags = await apiClient.get<ZetkinTag[]>(
`/api/orgs/${orgId}/people/tags`
);

if (!areaModel) {
return new NextResponse(null, { status: 404 });
}

const tags: ZetkinTag[] = [];
(areaModel.tags || []).forEach((item) => {
const tag = allTags.find((tag) => tag.id == item.id);
if (tag) {
tags.push({
...tag,
value: item.value,
});
}
});

const area: ZetkinArea = {
description: areaModel.description,
id: areaModel._id.toString(),
organization: {
id: orgId,
},
points: areaModel.points,
tags: tags,
title: areaModel.title,
};

@@ -50,7 +66,7 @@ export async function PATCH(request: NextRequest, { params }: RouteMeta) {
request: request,
roles: ['admin'],
},
async ({ orgId }) => {
async ({ apiClient, orgId }) => {
await mongoose.connect(process.env.MONGODB_URL || '');

const payload = await request.json();
@@ -69,6 +85,21 @@ export async function PATCH(request: NextRequest, { params }: RouteMeta) {
return new NextResponse(null, { status: 404 });
}

const allTags = await apiClient.get<ZetkinTag[]>(
`/api/orgs/${orgId}/people/tags`
);

const tags: ZetkinTag[] = [];
(model.tags || []).forEach((item) => {
const tag = allTags.find((tag) => tag.id == item.id);
if (tag) {
tags.push({
...tag,
value: item.value,
});
}
});

return NextResponse.json({
data: {
description: model.description,
@@ -77,6 +108,7 @@ export async function PATCH(request: NextRequest, { params }: RouteMeta) {
id: orgId,
},
points: model.points,
tags: tags,
title: model.title,
},
});
93 changes: 93 additions & 0 deletions src/app/beta/orgs/[orgId]/areas/[areaId]/tags/[tagId]/route.ts
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 });
}
);
}
49 changes: 49 additions & 0 deletions src/app/beta/orgs/[orgId]/areas/[areaId]/tags/route.ts
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();
}
}
);
}
40 changes: 30 additions & 10 deletions src/app/beta/orgs/[orgId]/areas/route.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { AreaModel } from 'features/areas/models';
import { ZetkinArea } from 'features/areas/types';
import asOrgAuthorized from 'utils/api/asOrgAuthorized';
import { ZetkinTag } from 'utils/types/zetkin';

type RouteMeta = {
params: {
@@ -18,19 +19,37 @@ export async function GET(request: NextRequest, { params }: RouteMeta) {
request: request,
roles: ['admin', 'organizer'],
},
async ({ orgId }) => {
async ({ apiClient, orgId }) => {
await mongoose.connect(process.env.MONGODB_URL || '');

const allTags = await apiClient.get<ZetkinTag[]>(
`/api/orgs/${orgId}/people/tags`
);

const areaModels = await AreaModel.find({ orgId });
const areas: ZetkinArea[] = areaModels.map((model) => ({
description: model.description,
id: model._id.toString(),
organization: {
id: orgId,
},
points: model.points,
title: model.title,
}));
const areas: ZetkinArea[] = areaModels.map((model) => {
const tags: ZetkinTag[] = [];
(model.tags || []).forEach((item) => {
const tag = allTags.find((tag) => tag.id == item.id);
if (tag) {
tags.push({
...tag,
value: item.value,
});
}
});

return {
description: model.description,
id: model._id.toString(),
organization: {
id: orgId,
},
points: model.points,
tags: tags,
title: model.title,
};
});

return Response.json({ data: areas });
}
@@ -66,6 +85,7 @@ export async function POST(request: NextRequest, { params }: RouteMeta) {
id: orgId,
},
points: model.points,
tags: [],
title: model.title,
},
});
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ export async function GET(request: NextRequest, { params }: RouteMeta) {
id: orgId,
},
points: area.points,
tags: [], // TODO: is this necessary here?
title: area.title,
},
assignee: person,
Loading

0 comments on commit 4392ce6

Please sign in to comment.