From e8a30e7251fdb6c0819886d21bd75bc109912cf6 Mon Sep 17 00:00:00 2001 From: Sergio Mensing <> Date: Wed, 5 Oct 2022 14:44:49 +0200 Subject: [PATCH 1/4] use slice instead of substring for desired result --- lib/format.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } From dcf16e1411e80bba2abfc25695d1c0585fa8d45e Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Thu, 6 Oct 2022 16:41:37 -0700 Subject: [PATCH 2/4] fix redis init --- lib/redis.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/redis.js b/lib/redis.js index 15ac27d9..01a9e3c6 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; From 2428314f584e5e21bf9764400bab6b3608f1d4ed Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Fri, 7 Oct 2022 17:13:03 -0700 Subject: [PATCH 3/4] Updated save method for websites. --- components/forms/WebsiteEditForm.js | 7 ++++-- pages/api/websites/[id]/index.js | 33 +++++++++++++++----------- queries/admin/website/updateWebsite.js | 6 ++--- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/components/forms/WebsiteEditForm.js b/components/forms/WebsiteEditForm.js index 21daeeaf..337e2d96 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} /> )} diff --git a/pages/api/websites/[id]/index.js b/pages/api/websites/[id]/index.js index 30592213..8234a5f2 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 (account_uuid) { + account = await getAccount({ account_uuid }); + } + + const website = await getWebsite(where); if (website.user_id !== currentUserId && !currentUserIsAdmin) { return unauthorized(res); } - let { share_id } = website; - - if (enable_share_url) { - share_id = share_id ? share_id : getRandomChars(8); - } else { - share_id = null; - } - - 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.js index 54f01f66..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(website_id, data) { +export async function updateWebsite(data, where) { return prisma.client.website.update({ - where: { - website_id, - }, + where, data, }); } From 6f6cbaa09805af605520c9a0b499aab2f1c0a57e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=B2=97?= Date: Sat, 8 Oct 2022 13:46:50 +0800 Subject: [PATCH 4/4] Update getWebsiteByUuid.js if redis website set from getWebsiteByUuid,the value is 1 forever --- queries/admin/website/getWebsiteByUuid.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/queries/admin/website/getWebsiteByUuid.js b/queries/admin/website/getWebsiteByUuid.js index 900e4539..1b1e04ca 100644 --- a/queries/admin/website/getWebsiteByUuid.js +++ b/queries/admin/website/getWebsiteByUuid.js @@ -10,7 +10,7 @@ export async function getWebsiteByUuid(website_uuid) { }) .then(async res => { if (redis.client && res) { - await redis.client.set(`website:${res.website_uuid}`, 1); + await redis.client.set(`website:${res.website_uuid}`, res.website_id); } return res;