Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'release-0.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
batamar committed Sep 28, 2017
2 parents 42428c9 + 6a94e42 commit bf52c22
Show file tree
Hide file tree
Showing 8 changed files with 1,029 additions and 297 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "erxes-app-api",
"version": "0.7.0",
"version": "0.8.0",
"description": "GraphQL API for erxes main project",
"homepage": "https://erxes.io",
"repository": "https://github.com/erxes/erxes-app-api",
Expand Down
2 changes: 2 additions & 0 deletions src/data/resolvers/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import engages from './engages';
import tags from './tags';
import customers from './customers';
import conversations from './conversations';
import insights from './insights';
import knowledgeBase from './knowledgeBase';

export default {
Expand All @@ -23,5 +24,6 @@ export default {
...tags,
...customers,
...conversations,
...insights,
...knowledgeBase,
};
200 changes: 200 additions & 0 deletions src/data/resolvers/queries/insightUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { Users, Integrations, Conversations } from '../../../db/models';
import moment from 'moment';

/**
* Builds messages find query selector.
* @param {Object} args
* @param {String} args.brandId
* @param {String} args.integrationType
* @param {Object} args.conversationSelector
* @param {Object} args.messageSelector
* @return {Promise} find input argument object.
*/
export const generateMessageSelector = async (
brandId,
integrationType,
conversationSelector,
messageSelector,
) => {
const selector = messageSelector;

const findConversationIds = async integrationSelector => {
const integrationIds = await Integrations.find(integrationSelector).select('_id');
const conversationIds = await Conversations.find({
...conversationSelector,
integrationId: { $in: integrationIds },
}).select('_id');

selector.conversationId = { $in: conversationIds };
};

const integrationSelector = {};

if (brandId) {
integrationSelector.brandId = brandId;
}

if (integrationType) {
integrationSelector.kind = integrationType;
}

await findConversationIds(integrationSelector);

return selector;
};

/**
* Populates message collection into date range
* by given duration and loop count for chart data.
* @param {Object} args
* @param {MessagesList} args.collection
* @param {Integer} args.loopCount
* @param {Integer} args.duration
* @param {Integer} args.starTime
* @return {[Object]} Chart data
*/
export const generateChartData = (collection, loopCount, duration, startTime) => {
const results = [];
let begin = 0;
let end = 0;
let count = 0;
let dateText = null;

// Variable that represents time interval by steps.
const divider = duration / loopCount;

for (let i = 0; i < loopCount; i++) {
end = startTime + divider * (i + 1);
begin = end - divider;
dateText = moment(begin).format('YYYY-MM-DD');

// messages count between begin and end time.
count = collection.filter(message => begin < message.createdAt && message.createdAt < end)
.length;

results.push({ name: dateText, count });
}

return results;
};

/**
* Generates time intervals for main report
* @param {Date} start
* @param {Date} end
* @return {Array} time intervals
*/
export const generateTimeIntervals = (start, end) => {
const month = moment(end).month();

return [
{
title: 'In time range',
start: moment(start),
end: moment(end),
},
{
title: 'This month',
start: moment(1, 'DD'),
end: moment(),
},
{
title: 'This week',
start: moment(end).weekday(0),
end: moment(end),
},
{
title: 'Today',
start: moment(end).add(-1, 'days'),
end: moment(end),
},
{
title: 'Last 30 days',
start: moment(end).add(-30, 'days'),
end: moment(end),
},
{
title: 'Last month',
start: moment(month, 'MM'),
end: moment(month + 1, 'MM'),
},
{
title: 'Last week',
start: moment(end).weekday(-7),
end: moment(end).weekday(0),
},
{
title: 'Yesterday',
start: moment(end).add(-2, 'days'),
end: moment(end).add(-1, 'days'),
},
];
};

/* Generate chart data for given user
* @param {String} userId
* @param {[Message]} userMessages
* @param {Number} duration
* @param {Number} startTime
* @return {Object} user detail informations with chart data
*/
export const generateUserChartData = async ({ userId, userMessages, duration, startTime }) => {
const user = await Users.findOne({ _id: userId });
const userData = generateChartData(userMessages, 5, duration, startTime);

if (!user) {
return {
graph: userData,
};
}

const userDetail = user.details;

return {
fullName: userDetail.fullName,
avatar: userDetail.avatar,
graph: userData,
};
};

export const formatTime = time => {
return time.format('YYYY-MM-DD HH:mm:ss');
};

export const getTime = time => {
return new Date(time).getTime();
};

export const fixDates = (startDate, endDate = new Date()) => {
if (!startDate || !endDate) {
const year = moment(endDate).year();
startDate = moment(endDate).year(year - 1);
}

return { start: startDate, end: endDate };
};

export const generateDuration = ({ start, end }) => {
const startTime = getTime(start);
const endTime = getTime(end);

return {
startTime,
endTime,
duration: endTime - startTime,
};
};

/* Determines user or client
* @param {String} type
* @return {Object} user selector
*/
export const generateUserSelector = type => {
let volumeOrResponse = null;

if (type === 'response') {
volumeOrResponse = { $ne: null };
}

return volumeOrResponse;
};
Loading

0 comments on commit bf52c22

Please sign in to comment.