2018-03-10 02:42:14 +08:00
|
|
|
import flat from 'flat';
|
|
|
|
import { Match, check } from 'meteor/check';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
import GroupChat from '/imports/api/group-chat';
|
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
export default async function addGroupChat(meetingId, chat) {
|
2018-03-10 02:42:14 +08:00
|
|
|
check(meetingId, String);
|
|
|
|
check(chat, {
|
|
|
|
id: Match.Maybe(String),
|
|
|
|
chatId: Match.Maybe(String),
|
|
|
|
correlationId: Match.Maybe(String),
|
|
|
|
access: String,
|
|
|
|
createdBy: Object,
|
|
|
|
users: Array,
|
|
|
|
msg: Match.Maybe(Array),
|
|
|
|
});
|
|
|
|
|
|
|
|
const chatDocument = {
|
|
|
|
meetingId,
|
|
|
|
chatId: chat.chatId || chat.id,
|
|
|
|
access: chat.access,
|
2022-04-01 21:16:15 +08:00
|
|
|
users: chat.users.map((u) => u.id),
|
2020-08-20 00:22:43 +08:00
|
|
|
participants: chat.users,
|
2018-03-10 02:42:14 +08:00
|
|
|
createdBy: chat.createdBy.id,
|
|
|
|
};
|
|
|
|
|
|
|
|
const selector = {
|
|
|
|
chatId: chatDocument.chatId,
|
|
|
|
meetingId,
|
|
|
|
};
|
|
|
|
|
|
|
|
const modifier = {
|
|
|
|
$set: flat(chatDocument, { safe: true }),
|
|
|
|
};
|
|
|
|
|
2020-11-23 21:13:46 +08:00
|
|
|
try {
|
2023-04-01 04:40:41 +08:00
|
|
|
const { insertedId } = await GroupChat.upsertAsync(selector, modifier);
|
2018-03-10 02:42:14 +08:00
|
|
|
|
|
|
|
if (insertedId) {
|
2022-05-17 20:39:15 +08:00
|
|
|
Logger.info(`Added group-chat chatId=${chatDocument.chatId} meetingId=${meetingId}`);
|
2020-11-23 21:13:46 +08:00
|
|
|
} else {
|
2022-05-17 20:39:15 +08:00
|
|
|
Logger.info(`Upserted group-chat chatId=${chatDocument.chatId} meetingId=${meetingId}`);
|
2018-03-10 02:42:14 +08:00
|
|
|
}
|
2020-11-23 21:13:46 +08:00
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Adding group-chat to collection: ${err}`);
|
|
|
|
}
|
2018-03-10 02:42:14 +08:00
|
|
|
}
|