mirror of
https://github.com/umami-software/umami.git
synced 2026-02-11 16:17:13 +01:00
website API shareId compatibility updates
Some checks are pending
Node.js CI / build (push) Waiting to run
Some checks are pending
Node.js CI / build (push) Waiting to run
This commit is contained in:
parent
fdb99607bc
commit
2489601ae6
4 changed files with 147 additions and 11 deletions
|
|
@ -1,8 +1,17 @@
|
|||
import { z } from 'zod';
|
||||
import { ENTITY_TYPE } from '@/lib/constants';
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json, ok, unauthorized } from '@/lib/response';
|
||||
import { badRequest, json, ok, serverError, unauthorized } from '@/lib/response';
|
||||
import { canDeleteWebsite, canUpdateWebsite, canViewWebsite } from '@/permissions';
|
||||
import { deleteWebsite, getWebsite, updateWebsite } from '@/queries/prisma';
|
||||
import {
|
||||
createShare,
|
||||
deleteSharesByEntityId,
|
||||
deleteWebsite,
|
||||
getShareByEntityId,
|
||||
getWebsite,
|
||||
updateWebsite,
|
||||
} from '@/queries/prisma';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
|
|
@ -32,6 +41,7 @@ export async function POST(
|
|||
const schema = z.object({
|
||||
name: z.string().optional(),
|
||||
domain: z.string().optional(),
|
||||
shareId: z.string().max(50).nullable().optional(),
|
||||
});
|
||||
|
||||
const { auth, body, error } = await parseRequest(request, schema);
|
||||
|
|
@ -41,15 +51,41 @@ export async function POST(
|
|||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { name, domain } = body;
|
||||
const { name, domain, shareId } = body;
|
||||
|
||||
if (!(await canUpdateWebsite(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const website = await updateWebsite(websiteId, { name, domain });
|
||||
try {
|
||||
const website = await updateWebsite(websiteId, { name, domain });
|
||||
|
||||
return Response.json(website);
|
||||
if (shareId === null) {
|
||||
await deleteSharesByEntityId(website.id);
|
||||
}
|
||||
|
||||
const share = shareId
|
||||
? await createShare({
|
||||
id: uuid(),
|
||||
entityId: websiteId,
|
||||
shareType: ENTITY_TYPE.website,
|
||||
name: website.name,
|
||||
slug: shareId,
|
||||
parameters: { overview: true, events: true },
|
||||
})
|
||||
: await getShareByEntityId(websiteId);
|
||||
|
||||
return json({
|
||||
...website,
|
||||
shareId: share?.slug ?? null,
|
||||
});
|
||||
} catch (e: any) {
|
||||
if (e.message.toLowerCase().includes('unique constraint')) {
|
||||
return badRequest({ message: 'That share ID is already taken.' });
|
||||
}
|
||||
|
||||
return serverError(e);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
import { z } from 'zod';
|
||||
import { ENTITY_TYPE } from '@/lib/constants';
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import { fetchAccount } from '@/lib/load';
|
||||
import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||
import { json, unauthorized } from '@/lib/response';
|
||||
import { pagingParams, searchParams } from '@/lib/schema';
|
||||
import { canCreateTeamWebsite, canCreateWebsite } from '@/permissions';
|
||||
import { createWebsite, getWebsiteCount } from '@/queries/prisma';
|
||||
import { createShare, createWebsite, getWebsiteCount } from '@/queries/prisma';
|
||||
import { getAllUserWebsitesIncludingTeamOwner, getUserWebsites } from '@/queries/prisma/website';
|
||||
|
||||
const CLOUD_WEBSITE_LIMIT = 3;
|
||||
|
|
@ -38,6 +39,7 @@ export async function POST(request: Request) {
|
|||
const schema = z.object({
|
||||
name: z.string().max(100),
|
||||
domain: z.string().max(500),
|
||||
shareId: z.string().max(50).nullable().optional(),
|
||||
teamId: z.uuid().nullable().optional(),
|
||||
id: z.uuid().nullable().optional(),
|
||||
});
|
||||
|
|
@ -48,7 +50,7 @@ export async function POST(request: Request) {
|
|||
return error();
|
||||
}
|
||||
|
||||
const { id, name, domain, teamId } = body;
|
||||
const { id, name, domain, shareId, teamId } = body;
|
||||
|
||||
if (process.env.CLOUD_MODE && !teamId) {
|
||||
const account = await fetchAccount(auth.user.id);
|
||||
|
|
@ -80,5 +82,19 @@ export async function POST(request: Request) {
|
|||
|
||||
const website = await createWebsite(data);
|
||||
|
||||
return json(website);
|
||||
const share = shareId
|
||||
? await createShare({
|
||||
id: uuid(),
|
||||
entityId: website.id,
|
||||
shareType: ENTITY_TYPE.website,
|
||||
name: website.name,
|
||||
slug: shareId,
|
||||
parameters: { overview: true, events: true },
|
||||
})
|
||||
: null;
|
||||
|
||||
return json({
|
||||
...website,
|
||||
shareId: share?.slug ?? null,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue