diff --git a/components/forms/WebsiteEditForm.js b/components/forms/WebsiteEditForm.js index 8c2f6de7..00cba540 100644 --- a/components/forms/WebsiteEditForm.js +++ b/components/forms/WebsiteEditForm.js @@ -78,7 +78,8 @@ export default function WebsiteEditForm({ values, onSave, onClose }) { const [message, setMessage] = useState(); const handleSubmit = async values => { - const { websiteId } = values; + const { id: websiteId } = values; + const { ok, data } = await post(websiteId ? `/websites/${websiteId}` : '/websites', values); if (ok) { @@ -137,7 +138,6 @@ export default function WebsiteEditForm({ values, onSave, onClose }) { defaultMessage="Enable share URL" /> } - value={null} /> )} diff --git a/lib/format.js b/lib/format.js index a85c3ef7..e138831d 100644 --- a/lib/format.js +++ b/lib/format.js @@ -74,7 +74,7 @@ export function stringToColor(str) { let color = '#'; for (let i = 0; i < 3; i++) { let value = (hash >> (i * 8)) & 0xff; - color += ('00' + value.toString(16)).substring(-2); + color += ('00' + value.toString(16)).slice(-2); } return color; } diff --git a/lib/redis.js b/lib/redis.js index 882cf414..85752c54 100644 --- a/lib/redis.js +++ b/lib/redis.js @@ -70,11 +70,7 @@ async function set(key, value) { async function connect() { if (!redis) { - process.env.REDIS_URL && (global[REDIS] || getClient()); - - if (!(await redis.get(INITIALIZED))) { - await stageData(); - } + redis = process.env.REDIS_URL && (global[REDIS] || getClient()); } return redis; diff --git a/pages/api/websites/[id]/index.js b/pages/api/websites/[id]/index.js index ea9acf59..c6b8779f 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, getRandomChars } 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,31 @@ export default async (req, res) => { if (req.method === 'POST') { await useAuth(req, res); - const { isAdmin: currentUserIsAdmin, userId: currentUserId } = req.auth; + const { isAdmin: currentUserIsAdmin, userId: currentUserId, accountUuid } = req.auth; const { name, domain, owner, enable_share_url } = req.body; + let account; - const website = await getWebsiteById(websiteId); + if (accountUuid) { + account = await getAccount({ accountUuid }); + } + + const website = await getWebsite(where); + + const shareId = enable_share_url ? website.shareId || getRandomChars(8) : null; if (website.userId !== currentUserId && !currentUserIsAdmin) { return unauthorized(res); } - let { shareId } = website; - - if (enable_share_url) { - shareId = shareId ? shareId : getRandomChars(8); - } else { - shareId = null; - } - - await updateWebsite(websiteId, { name, domain, shareId, userId: +owner }); + await updateWebsite( + { + name, + domain, + shareId: shareId, + userId: account ? account.id : +owner, + }, + where, + ); return ok(res); } diff --git a/pages/api/websites/index.js b/pages/api/websites/index.js index c076da45..00385f37 100644 --- a/pages/api/websites/index.js +++ b/pages/api/websites/index.js @@ -32,7 +32,7 @@ export default async (req, res) => { if (req.method === 'POST') { const { name, domain, owner, enable_share_url } = req.body; - const website_owner = account ? account.userId : +owner; + const website_owner = account ? account.id : +owner; if (website_owner !== currentUserId && !isAdmin) { return unauthorized(res); diff --git a/queries/admin/website/createWebsite.js b/queries/admin/website/createWebsite.js index 9d396c8c..7a45f1cf 100644 --- a/queries/admin/website/createWebsite.js +++ b/queries/admin/website/createWebsite.js @@ -15,7 +15,7 @@ export async function createWebsite(userId, data) { }) .then(async res => { if (redis.client && res) { - await redis.client.set(`website:${res.websiteUuid}`, res.websiteId); + await redis.client.set(`website:${res.websiteUuid}`, res.id); } return res; diff --git a/queries/admin/website/getWebsiteByUuid.js b/queries/admin/website/getWebsiteByUuid.js index 94d74ec9..158d357a 100644 --- a/queries/admin/website/getWebsiteByUuid.js +++ b/queries/admin/website/getWebsiteByUuid.js @@ -10,7 +10,7 @@ export async function getWebsiteByUuid(websiteUuid) { }) .then(async res => { if (redis.client && res) { - await redis.client.set(`website:${res.websiteUuid}`, res.websiteId); + await redis.client.set(`website:${res.websiteUuid}`, res.id); } return res; diff --git a/queries/admin/website/updateWebsite.js b/queries/admin/website/updateWebsite.js index 776cba29..1a5079a9 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(websiteId, data) { +export async function updateWebsite(data, where) { return prisma.client.website.update({ - where: { - id: websiteId, - }, + where, data, }); } diff --git a/queries/analytics/session/createSession.js b/queries/analytics/session/createSession.js index 3bc2cce0..6484dece 100644 --- a/queries/analytics/session/createSession.js +++ b/queries/analytics/session/createSession.js @@ -31,7 +31,7 @@ async function relationalQuery(websiteId, data) { }) .then(async res => { if (redis.client && res) { - await redis.client.set(`session:${res.sessionUuid}`, res.sessionId); + await redis.client.set(`session:${res.sessionUuid}`, res.id); } return res;