Chore: Use only LivechatTriggerRaw model (#23974)

* Use only LivechatTriggerRaw model

* Remove import
pull/23993/head
Diego Sampaio 4 years ago committed by GitHub
parent b1e1508b7c
commit 955d67e06f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/livechat/server/api/lib/livechat.js
  2. 7
      app/livechat/server/methods/getInitialData.js
  3. 10
      app/livechat/server/methods/removeTrigger.js
  4. 11
      app/livechat/server/methods/saveTrigger.js
  5. 2
      app/models/server/index.js
  6. 34
      app/models/server/models/LivechatTrigger.js
  7. 14
      app/models/server/raw/LivechatTrigger.ts
  8. 3
      app/models/server/raw/index.ts

@ -2,8 +2,8 @@ import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { LivechatRooms, LivechatVisitors, LivechatDepartment, LivechatTrigger } from '../../../../models/server';
import { EmojiCustom } from '../../../../models/server/raw';
import { LivechatRooms, LivechatVisitors, LivechatDepartment } from '../../../../models/server';
import { EmojiCustom, LivechatTrigger } from '../../../../models/server/raw';
import { Livechat } from '../../lib/Livechat';
import { callbacks } from '../../../../callbacks/server';
import { normalizeAgent } from '../../lib/Helper';
@ -12,8 +12,9 @@ export function online(department) {
return Livechat.online(department);
}
export function findTriggers() {
return LivechatTrigger.findEnabled().fetch().map(({ _id, actions, conditions, runOnce }) => ({ _id, actions, conditions, runOnce }));
async function findTriggers() {
const triggers = await LivechatTrigger.findEnabled().toArray();
return triggers.map(({ _id, actions, conditions, runOnce }) => ({ _id, actions, conditions, runOnce }));
}
export function findDepartments() {
@ -91,7 +92,7 @@ export function normalizeHttpHeaderData(headers = {}) {
}
export async function settings() {
const initSettings = Livechat.getInitSettings();
const triggers = findTriggers();
const triggers = await findTriggers();
const departments = findDepartments();
const sound = `${ Meteor.absoluteUrl() }sounds/chime.mp3`;
const emojis = await EmojiCustom.find().toArray();

@ -1,12 +1,13 @@
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';
import { LivechatRooms, Users, LivechatDepartment, LivechatTrigger, LivechatVisitors } from '../../../models';
import { LivechatRooms, Users, LivechatDepartment, LivechatVisitors } from '../../../models/server';
import { LivechatTrigger } from '../../../models/server/raw';
import { Livechat } from '../lib/Livechat';
import { deprecationWarning } from '../../../api/server/helpers/deprecationWarning';
Meteor.methods({
'livechat:getInitialData'(visitorToken, departmentId) {
async 'livechat:getInitialData'(visitorToken, departmentId) {
const info = {
enabled: null,
title: null,
@ -89,7 +90,7 @@ Meteor.methods({
info.agentData = room && room[0] && room[0].servedBy && Users.getAgentInfo(room[0].servedBy._id);
LivechatTrigger.findEnabled().forEach((trigger) => {
await LivechatTrigger.findEnabled().forEach((trigger) => {
info.triggers.push(_.pick(trigger, '_id', 'actions', 'conditions', 'runOnce'));
});

@ -1,17 +1,19 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { hasPermission } from '../../../authorization';
import { LivechatTrigger } from '../../../models';
import { hasPermission } from '../../../authorization/server';
import { LivechatTrigger } from '../../../models/server/raw';
Meteor.methods({
'livechat:removeTrigger'(triggerId) {
async 'livechat:removeTrigger'(triggerId) {
if (!Meteor.userId() || !hasPermission(Meteor.userId(), 'view-livechat-manager')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:removeTrigger' });
}
check(triggerId, String);
return LivechatTrigger.removeById(triggerId);
await LivechatTrigger.removeById(triggerId);
return true;
},
});

@ -2,10 +2,10 @@ import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { hasPermission } from '../../../authorization';
import { LivechatTrigger } from '../../../models';
import { LivechatTrigger } from '../../../models/server/raw';
Meteor.methods({
'livechat:saveTrigger'(trigger) {
async 'livechat:saveTrigger'(trigger) {
if (!Meteor.userId() || !hasPermission(Meteor.userId(), 'view-livechat-manager')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveTrigger' });
}
@ -21,8 +21,11 @@ Meteor.methods({
});
if (trigger._id) {
return LivechatTrigger.updateById(trigger._id, trigger);
await LivechatTrigger.updateById(trigger._id, trigger);
return true;
}
return LivechatTrigger.insert(trigger);
await LivechatTrigger.insertOne(trigger);
return true;
},
});

@ -11,7 +11,6 @@ import LivechatDepartment from './models/LivechatDepartment';
import LivechatDepartmentAgents from './models/LivechatDepartmentAgents';
import LivechatPageVisited from './models/LivechatPageVisited';
import LivechatRooms from './models/LivechatRooms';
import LivechatTrigger from './models/LivechatTrigger';
import LivechatVisitors from './models/LivechatVisitors';
import LivechatAgentActivity from './models/LivechatAgentActivity';
import LivechatInquiry from './models/LivechatInquiry';
@ -38,7 +37,6 @@ export {
LivechatDepartmentAgents,
LivechatPageVisited,
LivechatRooms,
LivechatTrigger,
LivechatVisitors,
LivechatAgentActivity,
LivechatExternalMessage,

@ -1,34 +0,0 @@
import { Base } from './_Base';
/**
* Livechat Trigger model
*/
export class LivechatTrigger extends Base {
constructor() {
super('livechat_trigger');
this.tryEnsureIndex({ enabled: 1 });
}
updateById(_id, data) {
return this.update({ _id }, { $set: data });
}
removeAll() {
return this.remove({});
}
findById(_id) {
return this.find({ _id });
}
removeById(_id) {
return this.remove({ _id });
}
findEnabled() {
return this.find({ enabled: true });
}
}
export default new LivechatTrigger();

@ -1,6 +1,18 @@
import { BaseRaw } from './BaseRaw';
import { Cursor, UpdateWriteOpResult } from 'mongodb';
import { BaseRaw, IndexSpecification } from './BaseRaw';
import { ILivechatTrigger } from '../../../../definition/ILivechatTrigger';
export class LivechatTriggerRaw extends BaseRaw<ILivechatTrigger> {
protected indexes: IndexSpecification[] = [
{ key: { enabled: 1 } },
];
findEnabled(): Cursor<ILivechatTrigger> {
return this.find({ enabled: true });
}
updateById(_id: string, data: ILivechatTrigger): Promise<UpdateWriteOpResult> {
return this.updateOne({ _id }, { $set: data });
}
}

@ -61,7 +61,6 @@ import LivechatDepartmentModel from '../models/LivechatDepartment';
import LivechatExternalMessagesModel from '../models/LivechatExternalMessages';
import LivechatInquiryModel from '../models/LivechatInquiry';
import LivechatRoomsModel from '../models/LivechatRooms';
import LivechatTriggerModel from '../models/LivechatTrigger';
import LivechatVisitorsModel from '../models/LivechatVisitors';
import LoginServiceConfigurationModel from '../models/LoginServiceConfiguration';
import MessagesModel from '../models/Messages';
@ -78,7 +77,6 @@ export const Subscriptions = new SubscriptionsRaw(SubscriptionsModel.model.rawCo
export const Settings = new SettingsRaw(SettingsModel.model.rawCollection(), trashCollection);
export const Rooms = new RoomsRaw(RoomsModel.model.rawCollection(), trashCollection);
export const LivechatCustomField = new LivechatCustomFieldRaw(LivechatCustomFieldModel.model.rawCollection(), trashCollection);
export const LivechatTrigger = new LivechatTriggerRaw(LivechatTriggerModel.model.rawCollection(), trashCollection);
export const LivechatDepartment = new LivechatDepartmentRaw(LivechatDepartmentModel.model.rawCollection(), trashCollection);
export const LivechatDepartmentAgents = new LivechatDepartmentAgentsRaw(LivechatDepartmentAgentsModel.model.rawCollection(), trashCollection);
export const LivechatRooms = new LivechatRoomsRaw(LivechatRoomsModel.model.rawCollection(), trashCollection);
@ -111,6 +109,7 @@ export const InstanceStatus = new InstanceStatusRaw(db.collection('instances'),
export const Integrations = new IntegrationsRaw(db.collection(`${ prefix }integrations`), trashCollection);
export const IntegrationHistory = new IntegrationHistoryRaw(db.collection(`${ prefix }integration_history`), trashCollection);
export const Invites = new InvitesRaw(db.collection(`${ prefix }invites`), trashCollection);
export const LivechatTrigger = new LivechatTriggerRaw(db.collection(`${ prefix }livechat_trigger`), trashCollection);
export const NotificationQueue = new NotificationQueueRaw(db.collection(`${ prefix }notification_queue`), trashCollection);
export const OAuthApps = new OAuthAppsRaw(db.collection(`${ prefix }oauth_apps`), trashCollection);
export const OEmbedCache = new OEmbedCacheRaw(db.collection(`${ prefix }oembed_cache`), trashCollection);

Loading…
Cancel
Save