Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
Updated save method for websites.
Mike Cao2428314f584e
diff --git a/components/forms/WebsiteEditForm.js b/components/forms/WebsiteEditForm.jsindex 21daeeaf8..337e2d96c 100644--- a/components/forms/WebsiteEditForm.js+++ b/components/forms/WebsiteEditForm.js@@ -14,6 +14,7 @@ import useApi from 'hooks/useApi';import useFetch from 'hooks/useFetch';import useUser from 'hooks/useUser';import styles from './WebsiteEditForm.module.css';+import { getRandomChars } from 'next-basics';const initialValues = {name: '',@@ -78,7 +79,10 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {const [message, setMessage] = useState();const handleSubmit = async values => {- const { website_id } = values;+ const { website_id, enable_share_url, share_id } = values;+ if (enable_share_url) {+ values.share_id = share_id || getRandomChars(8);+ }const { ok, data } = await post(website_id ? `/websites/${website_id}` : '/websites', values);if (ok) {@@ -137,7 +141,6 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {defaultMessage="Enable share URL"/>}- value={null}/>)}</Field>diff --git a/pages/api/websites/[id]/index.js b/pages/api/websites/[id]/index.jsindex 30592213d..8234a5f20 100644--- a/pages/api/websites/[id]/index.js+++ b/pages/api/websites/[id]/index.js@@ -1,5 +1,5 @@-import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';-import { deleteWebsite, getWebsite, getWebsiteById, updateWebsite } from 'queries';+import { methodNotAllowed, ok, unauthorized } from 'next-basics';+import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';import { allowQuery } from 'lib/auth';import { useAuth, useCors } from 'lib/middleware';import { validate } from 'uuid';@@ -25,24 +25,29 @@ export default async (req, res) => {if (req.method === 'POST') {await useAuth(req, res);- const { is_admin: currentUserIsAdmin, user_id: currentUserId } = req.auth;- const { name, domain, owner, enable_share_url } = req.body;+ const { is_admin: currentUserIsAdmin, user_id: currentUserId, account_uuid } = req.auth;+ const { name, domain, owner, share_id } = req.body;+ let account;- const website = await getWebsiteById(websiteId);-- if (website.user_id !== currentUserId && !currentUserIsAdmin) {- return unauthorized(res);+ if (account_uuid) {+ account = await getAccount({ account_uuid });}- let { share_id } = website;+ const website = await getWebsite(where);- if (enable_share_url) {- share_id = share_id ? share_id : getRandomChars(8);- } else {- share_id = null;+ if (website.user_id !== currentUserId && !currentUserIsAdmin) {+ return unauthorized(res);}- await updateWebsite(websiteId, { name, domain, share_id, user_id: +owner });+ await updateWebsite(+ {+ name,+ domain,+ share_id: share_id || null,+ user_id: account ? account.id : +owner,+ },+ where,+ );return ok(res);}diff --git a/queries/admin/website/updateWebsite.js b/queries/admin/website/updateWebsite.jsindex 54f01f66d..1a5079a9a 100644--- a/queries/admin/website/updateWebsite.js+++ b/queries/admin/website/updateWebsite.js@@ -1,10 +1,8 @@import prisma from 'lib/prisma';-export async function updateWebsite(website_id, data) {+export async function updateWebsite(data, where) {return prisma.client.website.update({- where: {- website_id,- },+ where,data,});}