feat(db & e2e): Enhance DB Schemas/Controllers and Improve E2E Tests (#966)

* feat: add global teardown to remove test data and add registration/log-out to auth flow

* refactor(models/Conversation): index user field and add JSDoc to deleteConvos

* refactor: add user index to message schema and ensure user is saved to each Message

* refactor: add user to each saveMessage call

* fix: handle case where title is null in zod schema

* feat(e2e): ensure messages are deleted on cleanUp

* fix: set last convo for all endpoints on conversation update

* fix: enable registration for CI env
This commit is contained in:
Danny Avila 2023-09-18 15:19:50 -04:00 committed by GitHub
parent fd70e21732
commit 6358383001
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 229 additions and 72 deletions

View file

@ -108,6 +108,23 @@ module.exports = {
return { message: 'Error getting conversation title' };
}
},
/**
* Asynchronously deletes conversations and associated messages for a given user and filter.
*
* @async
* @function
* @param {string|ObjectId} user - The user's ID.
* @param {Object} filter - Additional filter criteria for the conversations to be deleted.
* @returns {Promise<{ n: number, ok: number, deletedCount: number, messages: { n: number, ok: number, deletedCount: number } }>}
* An object containing the count of deleted conversations and associated messages.
* @throws {Error} Throws an error if there's an issue with the database operations.
*
* @example
* const user = 'someUserId';
* const filter = { someField: 'someValue' };
* const result = await deleteConvos(user, filter);
* console.log(result); // { n: 5, ok: 1, deletedCount: 5, messages: { n: 10, ok: 1, deletedCount: 10 } }
*/
deleteConvos: async (user, filter) => {
let toRemove = await Conversation.find({ ...filter, user }).select('conversationId');
const ids = toRemove.map((instance) => instance.conversationId);

View file

@ -7,6 +7,7 @@ module.exports = {
Message,
async saveMessage({
user,
messageId,
newMessageId,
conversationId,
@ -33,6 +34,7 @@ module.exports = {
await Message.findOneAndUpdate(
{ messageId },
{
user,
messageId: newMessageId || messageId,
conversationId,
parentMessageId,

View file

@ -17,6 +17,7 @@ const convoSchema = mongoose.Schema(
},
user: {
type: String,
index: true,
default: null,
},
messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }],

View file

@ -14,6 +14,11 @@ const messageSchema = mongoose.Schema(
required: true,
meiliIndex: true,
},
user: {
type: String,
index: true,
default: null,
},
model: {
type: String,
},