autonomous.contact/public/api/zones/index.ts

118 lines
3.1 KiB
TypeScript
Raw Normal View History

import lurid from '@andyburke/lurid';
import parse_body from '../../../utils/bodyparser.ts';
import { get_session, get_user, require_user } from '../../../utils/prechecks.ts';
import * as CANNED_RESPONSES from '../../../utils/canned_responses.ts';
import { PRECHECK_TABLE } from '../../../utils/prechecks.ts';
2025-09-09 15:32:07 -07:00
import { ZONE, ZONES } from '../../../models/zone.ts';
import { WALK_ENTRY } from '@andyburke/fsdb';
export const PRECHECKS: PRECHECK_TABLE = {};
2025-09-09 15:32:07 -07:00
// GET /api/zones - get zones
PRECHECKS.GET = [get_session, get_user, require_user, (_req: Request, meta: Record<string, any>): Response | undefined => {
2025-09-09 15:32:07 -07:00
const can_read_zones = meta.user.permissions.includes('zones.read');
2025-09-09 15:32:07 -07:00
if (!can_read_zones) {
return CANNED_RESPONSES.permission_denied();
}
}];
export async function GET(_req: Request, meta: Record<string, any>): Promise<Response> {
const limit = Math.min(parseInt(meta.query.limit ?? '100'), 100);
2025-09-09 15:32:07 -07:00
const zones = (await ZONES.all({
2025-07-04 12:15:06 -07:00
limit,
2025-09-09 15:32:07 -07:00
filter: (entry: WALK_ENTRY<ZONE>) => {
// we push our event collections into the zones, and fsdb
2025-07-04 12:15:06 -07:00
// doesn't yet filter that out in its all() logic
return entry.path.indexOf('/events/') === -1;
}
2025-09-09 15:32:07 -07:00
})).map((zone_entry) => zone_entry.load());
2025-09-09 15:32:07 -07:00
return Response.json(zones, {
status: 200
});
}
2025-09-09 15:32:07 -07:00
// POST /api/zones - Create a zone
PRECHECKS.POST = [get_session, get_user, require_user, (_req: Request, meta: Record<string, any>): Response | undefined => {
2025-09-09 15:32:07 -07:00
const can_create_zones = meta.user.permissions.includes('zones.create');
2025-09-09 15:32:07 -07:00
if (!can_create_zones) {
return CANNED_RESPONSES.permission_denied();
}
}];
export async function POST(req: Request, meta: Record<string, any>): Promise<Response> {
try {
const now = new Date().toISOString();
const body = await parse_body(req);
if (typeof body.name !== 'string' || body.name.length === 0) {
return Response.json({
error: {
2025-09-09 15:32:07 -07:00
cause: 'missing_zone_name',
message: 'You must specify a unique name for a zone.'
}
}, {
status: 400
});
}
if (body.name.length > 64) {
return Response.json({
error: {
2025-09-09 15:32:07 -07:00
cause: 'invalid_zone_name',
message: 'zone names must be 64 characters or fewer.'
}
}, {
status: 400
});
}
const normalized_name = body.name.toLowerCase();
2025-09-09 15:32:07 -07:00
const existing_zone = (await ZONES.find({
name: normalized_name
})).shift();
2025-09-09 15:32:07 -07:00
if (existing_zone) {
return Response.json({
error: {
2025-09-09 15:32:07 -07:00
cause: 'zone_name_conflict',
message: 'There is already a zone with this name.'
}
}, {
status: 400
});
}
2025-09-09 15:32:07 -07:00
const zone: ZONE = {
...body,
id: lurid(),
creator_id: meta.user.id,
permissions: {
read: (body.permissions?.read ?? []),
write: (body.permissions?.write ?? [meta.user.id]),
read_events: (body.permissions?.read_events ?? []),
write_events: (body.permissions?.write_events ?? [])
},
timestamps: {
created: now,
updated: now,
archived: undefined
}
};
2025-09-09 15:32:07 -07:00
await ZONES.create(zone);
2025-09-09 15:32:07 -07:00
return Response.json(zone, {
status: 201
});
} catch (error) {
return Response.json({
error: {
message: (error as Error).message ?? 'Unknown Error!',
cause: (error as Error).cause ?? 'unknown'
}
}, { status: 500 });
}
}