patch bot keys

This commit is contained in:
Maidul Islam
2024-01-27 12:40:36 +05:30
committed by Akhil Mohan
parent 35eade0206
commit caabf2c952
+23 -23
View File
@@ -592,16 +592,6 @@ const main = async () => {
return envKv.sublevel(environment); return envKv.sublevel(environment);
}; };
const checkIfFolderIsDangling = async (
projectId: string,
env_slug: string,
folderId: string,
) => {
const kv = getFolderKv(projectId, truncateAndSlugify(env_slug));
const result = await kv.get(`${folderId}:dead`).catch(() => null);
return Boolean(result);
};
await migrateCollection({ await migrateCollection({
db, db,
mongooseCollection: Workspace, mongooseCollection: Workspace,
@@ -1300,16 +1290,26 @@ const main = async () => {
.catch(() => null); .catch(() => null);
if (!projectKvRes) return; if (!projectKvRes) return;
await projectBotKv.put(doc._id.toString(), id); // if we try to process two bots for the same workspace, then skip
if (await projectBotKv.get(doc.workspace.toString()).catch(() => null)){
return
}
await projectBotKv.put(doc.workspace.toString(), id);
// case: skip bots that are inactive (skipped specifically because 6388653a200193a667c7e3f3 has two records, one active one not) // case: skip bots that are inactive (skipped specifically because 6388653a200193a667c7e3f3 has two records, one active one not)
let botKey = await BotKey.findOne({ let bot = await Bot.findOne({
workspace: doc.workspace, workspace: doc.workspace,
$or: [{ isActive: true }, { isActive: false }], $or: [{ isActive: true }, { isActive: false }],
}) })
.sort({ isActive: -1 }) .sort({ isActive: -1 })
.lean(); .lean();
// case: when no bots are found for this project, skip
if (!bot) return
const botKey = await BotKey.findOne({bot: bot?._id})
const senderId = botKey?.sender const senderId = botKey?.sender
? await userKv.get(botKey.sender.toString()).catch(() => null) ? await userKv.get(botKey.sender.toString()).catch(() => null)
: null; : null;
@@ -1318,20 +1318,20 @@ const main = async () => {
return { return {
id, id,
algorithm: doc.algorithm, algorithm: bot.algorithm,
keyEncoding: doc.keyEncoding, keyEncoding: bot.keyEncoding,
projectId: doc.workspace.toString(), projectId: bot.workspace.toString(),
name: doc.name, name: bot.name,
iv: doc.iv, iv: bot.iv,
tag: doc.tag, tag: bot.tag,
senderId, senderId,
isActive: doc.isActive, isActive: bot.isActive,
publicKey: doc.publicKey, publicKey: bot.publicKey,
encryptedProjectKey: botKey?.encryptedKey || null, encryptedProjectKey: botKey?.encryptedKey || null,
encryptedProjectKeyNonce: botKey?.nonce || null, encryptedProjectKeyNonce: botKey?.nonce || null,
encryptedPrivateKey: doc.encryptedPrivateKey, encryptedPrivateKey: bot.encryptedPrivateKey,
createdAt: new Date((doc as any).createdAt), createdAt: new Date((bot as any).createdAt),
updatedAt: new Date((doc as any).updatedAt), updatedAt: new Date((bot as any).updatedAt),
}; };
}, },
}); });