From 77bcb80e0090082e4c136ac50d68d40ff4573fc7 Mon Sep 17 00:00:00 2001 From: normunds-wipo <158038420+normunds-wipo@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:17:21 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20fix:=20Remove=20`expire?= =?UTF-8?q?sAt`=20field=20when=20setting=20expiry=20to=20"never"=20(#4294)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server/services/UserService.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/api/server/services/UserService.js b/api/server/services/UserService.js index 30b54c7406..91d772477b 100644 --- a/api/server/services/UserService.js +++ b/api/server/services/UserService.js @@ -103,10 +103,10 @@ const getUserKeyExpiry = async ({ userId, name }) => { * @param {string} params.userId - The unique identifier for the user. * @param {string} params.name - The name associated with the key. * @param {string} params.value - The value to be encrypted and stored as the key's value. - * @param {Date} params.expiresAt - The expiry date for the key. + * @param {Date} params.expiresAt - The expiry date for the key [optional] * @returns {Promise} The updated or newly inserted key document. * @description This function either updates an existing user key or inserts a new one into the database, - * after encrypting the provided value. It sets the provided expiry date for the key. + * after encrypting the provided value. It sets the provided expiry date for the key (or unsets for no expiry). */ const updateUserKey = async ({ userId, name, value, expiresAt = null }) => { const encryptedValue = await encrypt(value); @@ -115,13 +115,15 @@ const updateUserKey = async ({ userId, name, value, expiresAt = null }) => { name, value: encryptedValue, }; - - // Only add expiresAt to the update object if it's not null + const updateQuery = { $set: updateObject }; + // add expiresAt to the update object if it's not null if (expiresAt) { updateObject.expiresAt = new Date(expiresAt); + } else { + // make sure to remove if already present + updateQuery.$unset = { expiresAt }; } - - return await Key.findOneAndUpdate({ userId, name }, updateObject, { + return await Key.findOneAndUpdate({ userId, name }, updateQuery, { upsert: true, new: true, }).lean();