fix: hide logs when fetched items are sent back on saveConversation

pull/2756/head
Audric Ackermann 2 years ago
parent 77e5e38e46
commit c42f828044

@ -163,6 +163,9 @@ export function formatRowOfConversation(
}; };
} }
/**
* Those attributes are the one we are sending to the sql call as we want to save them when saving a conversation row.
*/
const allowedKeysOfConversationAttributes = [ const allowedKeysOfConversationAttributes = [
'groupAdmins', 'groupAdmins',
'members', 'members',
@ -191,6 +194,17 @@ const allowedKeysOfConversationAttributes = [
'priority', 'priority',
]; ];
/**
* Those attributes are the one we know the renderer is sending back but which we do not want to save to the database.
* They are fetched when getting the conversation from the DB and in anything returning a SaveConversationReturn
*/
const allowedKeysButNotSavedToDb = ['mentionedUs', 'unreadCount'];
/**
* This one merges each list together, and must be used for the log statement only.
*/
const allowedKeysTogether = [...allowedKeysOfConversationAttributes, ...allowedKeysButNotSavedToDb];
/** /**
* assertValidConversationAttributes is used to make sure that only the keys stored in the database are sent from the renderer. * assertValidConversationAttributes is used to make sure that only the keys stored in the database are sent from the renderer.
* We could also add some type checking here to make sure what is sent by the renderer matches what we expect to store in the DB * We could also add some type checking here to make sure what is sent by the renderer matches what we expect to store in the DB
@ -198,22 +212,17 @@ const allowedKeysOfConversationAttributes = [
export function assertValidConversationAttributes( export function assertValidConversationAttributes(
data: ConversationAttributes data: ConversationAttributes
): ConversationAttributes { ): ConversationAttributes {
// first make sure all keys of the object data are expected to be there // first make sure all keys of the object data are expected to be there, or expected to not be saved to the DB
const foundInAttributesButNotInAllowed = difference( const foundInAttributesButNotInAllowed = difference(Object.keys(data), allowedKeysTogether);
Object.keys(data),
allowedKeysOfConversationAttributes
);
if (foundInAttributesButNotInAllowed?.length) { if (foundInAttributesButNotInAllowed?.length) {
// tslint:disable-next-line: no-console // tslint:disable-next-line: no-console
console.error( console.error(
`assertValidConversationAttributes: an invalid key was given in the record: ${foundInAttributesButNotInAllowed}` `assertValidConversationAttributes: an invalid key was given in the record: ${foundInAttributesButNotInAllowed}`
); );
// throw new Error(
// `assertValidConversationAttributes: found a not allowed key: ${foundInAttributesButNotInAllowed[0]}`
// );
} }
// we only ever want to save the allowedKeysOfConversationAttributes here, not the one part of allowedKeysButNotSavedToDb
return pick(data, allowedKeysOfConversationAttributes) as ConversationAttributes; return pick(data, allowedKeysOfConversationAttributes) as ConversationAttributes;
} }

Loading…
Cancel
Save