|
|
|
|
@ -1,6 +1,8 @@ |
|
|
|
|
import type { IEmailInbox } from '@rocket.chat/core-typings'; |
|
|
|
|
import { EmailInbox, Users } from '@rocket.chat/models'; |
|
|
|
|
import type { Filter, InsertOneResult, Sort, UpdateResult, WithId } from 'mongodb'; |
|
|
|
|
import type { DeleteResult, Filter, InsertOneResult, Sort } from 'mongodb'; |
|
|
|
|
|
|
|
|
|
import { notifyOnEmailInboxChanged } from '../../../lib/server/lib/notifyListener'; |
|
|
|
|
|
|
|
|
|
export const findEmailInboxes = async ({ |
|
|
|
|
query = {}, |
|
|
|
|
@ -34,33 +36,31 @@ export const findEmailInboxes = async ({ |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const findOneEmailInbox = async ({ _id }: { _id: string }): Promise<IEmailInbox | null> => { |
|
|
|
|
return EmailInbox.findOneById(_id); |
|
|
|
|
}; |
|
|
|
|
export const insertOneEmailInbox = async ( |
|
|
|
|
userId: string, |
|
|
|
|
emailInboxParams: Pick<IEmailInbox, 'active' | 'name' | 'email' | 'description' | 'senderInfo' | 'department' | 'smtp' | 'imap'>, |
|
|
|
|
): Promise<InsertOneResult<WithId<IEmailInbox>>> => { |
|
|
|
|
): Promise<InsertOneResult<IEmailInbox>> => { |
|
|
|
|
const obj = { |
|
|
|
|
...emailInboxParams, |
|
|
|
|
_createdAt: new Date(), |
|
|
|
|
_updatedAt: new Date(), |
|
|
|
|
_createdBy: await Users.findOneById(userId, { projection: { username: 1 } }), |
|
|
|
|
}; |
|
|
|
|
return EmailInbox.insertOne(obj); |
|
|
|
|
|
|
|
|
|
const response = await EmailInbox.create(obj); |
|
|
|
|
|
|
|
|
|
if (response.insertedId) { |
|
|
|
|
void notifyOnEmailInboxChanged({ _id: response.insertedId, ...obj }, 'inserted'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const updateEmailInbox = async ( |
|
|
|
|
emailInboxParams: Pick<IEmailInbox, '_id' | 'active' | 'name' | 'email' | 'description' | 'senderInfo' | 'department' | 'smtp' | 'imap'>, |
|
|
|
|
): Promise<InsertOneResult<WithId<IEmailInbox>> | UpdateResult> => { |
|
|
|
|
): Promise<Pick<IEmailInbox, '_id'> | null> => { |
|
|
|
|
const { _id, active, name, email, description, senderInfo, department, smtp, imap } = emailInboxParams; |
|
|
|
|
|
|
|
|
|
const emailInbox = await findOneEmailInbox({ _id }); |
|
|
|
|
|
|
|
|
|
if (!emailInbox) { |
|
|
|
|
throw new Error('error-invalid-email-inbox'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const updateEmailInbox = { |
|
|
|
|
$set: { |
|
|
|
|
active, |
|
|
|
|
@ -76,5 +76,29 @@ export const updateEmailInbox = async ( |
|
|
|
|
...(department === 'All' && { $unset: { department: 1 as const } }), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return EmailInbox.updateOne({ _id }, updateEmailInbox); |
|
|
|
|
const updatedResponse = await EmailInbox.updateById(_id, updateEmailInbox); |
|
|
|
|
|
|
|
|
|
if (!updatedResponse.value) { |
|
|
|
|
throw new Error('error-invalid-email-inbox'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void notifyOnEmailInboxChanged( |
|
|
|
|
{ |
|
|
|
|
...updatedResponse.value, |
|
|
|
|
...(department === 'All' && { department: undefined }), |
|
|
|
|
}, |
|
|
|
|
'updated', |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
return updatedResponse.value; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const removeEmailInbox = async (emailInboxId: IEmailInbox['_id']): Promise<DeleteResult> => { |
|
|
|
|
const removeResponse = await EmailInbox.removeById(emailInboxId); |
|
|
|
|
|
|
|
|
|
if (removeResponse.deletedCount) { |
|
|
|
|
void notifyOnEmailInboxChanged({ _id: emailInboxId }, 'removed'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return removeResponse; |
|
|
|
|
}; |
|
|
|
|
|