From 8decbc20d5034b4d6604525e8a0d93efaa4fff98 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 1 Oct 2021 15:00:11 +0100 Subject: [PATCH] Fixing issue with created by/updated by on public forms, also fixing a small issue with analytics ping generating a 404. --- packages/server/src/api/controllers/analytics.js | 11 ++++++++--- packages/server/src/api/routes/analytics.js | 5 +++-- packages/server/src/utilities/rowProcessor/index.js | 10 ++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/controllers/analytics.js b/packages/server/src/api/controllers/analytics.js index ef410aace5..e6e5d6a748 100644 --- a/packages/server/src/api/controllers/analytics.js +++ b/packages/server/src/api/controllers/analytics.js @@ -7,14 +7,19 @@ if (env.POSTHOG_TOKEN && env.ENABLE_ANALYTICS && !env.SELF_HOSTED) { posthogClient = new PostHog(env.POSTHOG_TOKEN) } -exports.isEnabled = async function (ctx) { +exports.isEnabled = async ctx => { ctx.body = { enabled: !env.SELF_HOSTED && env.ENABLE_ANALYTICS === "true", } } -exports.endUserPing = async (ctx, next) => { - if (!posthogClient) return next() +exports.endUserPing = async ctx => { + if (!posthogClient) { + ctx.body = { + ping: false, + } + return + } posthogClient.capture("budibase:end_user_ping", { userId: ctx.user && ctx.user._id, diff --git a/packages/server/src/api/routes/analytics.js b/packages/server/src/api/routes/analytics.js index 632e99d031..be27e7d19b 100644 --- a/packages/server/src/api/routes/analytics.js +++ b/packages/server/src/api/routes/analytics.js @@ -3,7 +3,8 @@ const controller = require("../controllers/analytics") const router = Router() -router.get("/api/analytics", controller.isEnabled) -router.post("/api/analytics/ping", controller.endUserPing) +router + .get("/api/analytics", controller.isEnabled) + .post("/api/analytics/ping", controller.endUserPing) module.exports = router diff --git a/packages/server/src/utilities/rowProcessor/index.js b/packages/server/src/utilities/rowProcessor/index.js index 07549dd8a8..55af87bc13 100644 --- a/packages/server/src/utilities/rowProcessor/index.js +++ b/packages/server/src/utilities/rowProcessor/index.js @@ -99,6 +99,7 @@ function processAutoColumn( row, opts = { reprocessing: false, noAutoRelationships: false } ) { + let noUser = !user || !user.userId let now = new Date().toISOString() // if a row doesn't have a revision then it doesn't exist yet const creating = !row._rev @@ -108,7 +109,12 @@ function processAutoColumn( } switch (schema.subtype) { case AutoFieldSubTypes.CREATED_BY: - if (creating && !opts.reprocessing && !opts.noAutoRelationships) { + if ( + creating && + !opts.reprocessing && + !opts.noAutoRelationships && + !noUser + ) { row[key] = [user.userId] } break @@ -118,7 +124,7 @@ function processAutoColumn( } break case AutoFieldSubTypes.UPDATED_BY: - if (!opts.reprocessing && !opts.noAutoRelationships) { + if (!opts.reprocessing && !opts.noAutoRelationships && !noUser) { row[key] = [user.userId] } break