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

Commit 81e3880

Browse files
author
BatAmar Battulga
committed
perf(widgets): merge erxes-widgets-api with erxes-api
BREAKING CHANGE: erxes-widgets-api is making code duplication difficult to maintain and we decided that it is an unnecessary abstraction. 1. Remove MAIN_API_URL env variable 2. Point API_GRAPHQL_URL env variable to http://localhost:3300/graphql AKA erxes-api close #1542
1 parent 09658b5 commit 81e3880

40 files changed

+3822
-60
lines changed

src/__tests__/conversationDb.test.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { conversationFactory, conversationMessageFactory, customerFactory, userFactory } from '../db/factories';
1+
import {
2+
conversationFactory,
3+
conversationMessageFactory,
4+
customerFactory,
5+
engageDataFactory,
6+
userFactory,
7+
} from '../db/factories';
28
import { ConversationMessages, Conversations, Users } from '../db/models';
39
import { CONVERSATION_STATUSES } from '../db/models/definitions/constants';
410

@@ -15,6 +21,7 @@ describe('Conversation db', () => {
1521
_conversation = await conversationFactory({});
1622
_conversationMessage = await conversationMessageFactory({
1723
conversationId: _conversation._id,
24+
internal: false,
1825
content: 'content',
1926
});
2027

@@ -387,4 +394,68 @@ describe('Conversation db', () => {
387394
expect(await Conversations.find({ customerId: customer._id })).toHaveLength(0);
388395
expect(await ConversationMessages.find({ conversationId: conversation._id })).toHaveLength(0);
389396
});
397+
398+
test('forceReadCustomerPreviousEngageMessages', async () => {
399+
const customerId = '_id';
400+
401+
// isCustomRead is defined ===============
402+
await conversationMessageFactory({
403+
customerId,
404+
engageData: engageDataFactory({ messageId: '_id' }),
405+
isCustomerRead: false,
406+
});
407+
408+
await ConversationMessages.forceReadCustomerPreviousEngageMessages(customerId);
409+
410+
let messages = await ConversationMessages.find({
411+
customerId,
412+
engageData: { $exists: true },
413+
isCustomerRead: true,
414+
});
415+
416+
expect(messages.length).toBe(1);
417+
418+
// isCustomRead is undefined ===============
419+
await ConversationMessages.deleteMany({});
420+
421+
await conversationMessageFactory({
422+
customerId,
423+
engageData: engageDataFactory({ messageId: '_id' }),
424+
});
425+
426+
await ConversationMessages.forceReadCustomerPreviousEngageMessages(customerId);
427+
428+
messages = await ConversationMessages.find({
429+
customerId,
430+
engageData: { $exists: true },
431+
isCustomerRead: true,
432+
});
433+
434+
expect(messages.length).toBe(1);
435+
});
436+
437+
test('widgetsUnreadMessagesQuery', async () => {
438+
const conversation = await conversationFactory({});
439+
440+
const response = await Conversations.widgetsUnreadMessagesQuery([conversation]);
441+
442+
expect(JSON.stringify(response)).toBe(
443+
JSON.stringify({
444+
conversationId: { $in: [conversation._id] },
445+
userId: { $exists: true },
446+
internal: false,
447+
isCustomerRead: { $ne: true },
448+
}),
449+
);
450+
});
451+
452+
test('updateConversation', async () => {
453+
const conversation = await conversationFactory({});
454+
455+
await Conversations.updateConversation(conversation._id, { content: 'updated' });
456+
457+
const updated = await Conversations.findOne({ _id: conversation._id });
458+
459+
expect(updated && updated.content).toBe('updated');
460+
});
390461
});

src/__tests__/customerDb.test.ts

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from '../db/models';
2121
import { ACTIVITY_CONTENT_TYPES, STATUSES } from '../db/models/definitions/constants';
2222

23+
import { ICustomerDocument } from '../db/models/definitions/customers';
2324
import './setup.ts';
2425

2526
describe('Customers model tests', () => {
@@ -492,4 +493,244 @@ describe('Customers model tests', () => {
492493

493494
expect(response.messengerData && response.messengerData.isActive).toBeTruthy();
494495
});
496+
497+
test('createMessengerCustomer() must return a new customer', async () => {
498+
const now = new Date();
499+
500+
const firstName = 'test first name';
501+
const lastName = 'test last name';
502+
const bio = 'test BIO 1231321312';
503+
const email = 'uniqueEmail@gmail.com';
504+
const phone = '422999';
505+
506+
const customData = {
507+
first_name: firstName,
508+
last_name: lastName,
509+
bio,
510+
created_at: new Date(),
511+
};
512+
513+
const customer = await Customers.createMessengerCustomer(
514+
{
515+
integrationId: _customer.integrationId,
516+
email,
517+
phone,
518+
isUser: _customer.isUser,
519+
},
520+
customData,
521+
);
522+
523+
expect(customer).toBeDefined();
524+
525+
expect(customer.createdAt).toBeDefined();
526+
expect(customer.modifiedAt).toBeDefined();
527+
expect(customer.primaryEmail).toBe(email);
528+
expect(customer.emails).toContain(email);
529+
530+
expect(customer.primaryPhone).toBe(phone);
531+
expect(customer.phones).toContain(phone);
532+
533+
expect(customer.isUser).toBe(_customer.isUser);
534+
expect(customer.createdAt >= now).toBe(true);
535+
expect(customer.lastName).toBe(lastName);
536+
expect(customer.description).toBe(bio);
537+
538+
const messengerData = customer.messengerData;
539+
540+
if (!messengerData) {
541+
throw new Error('messengerData is null');
542+
}
543+
544+
expect(messengerData.lastSeenAt).toBeDefined();
545+
expect(messengerData.isActive).toBe(true);
546+
expect(messengerData.sessionCount).toBe(1);
547+
expect(messengerData.customData.first_name).toBeUndefined();
548+
expect(messengerData.customData.last_name).toBeUndefined();
549+
expect(messengerData.customData.bio).toBeUndefined();
550+
expect(messengerData.customData.created_at).toBeDefined();
551+
});
552+
553+
test('updateMessengerCustomer()', async () => {
554+
try {
555+
await Customers.updateMessengerCustomer({ _id: '_id', doc: {}, customData: {} });
556+
} catch (e) {
557+
expect(e.message).toBe('Customer not found');
558+
}
559+
560+
const firstName = 'test first name';
561+
const lastName = 'test last name';
562+
const bio = 'test BIO 1231321312';
563+
const email = 'uniqueEmail@gmail.com';
564+
const phone = '422999';
565+
const deviceToken = 'token';
566+
567+
const customData = {
568+
first_name: firstName,
569+
last_name: lastName,
570+
bio,
571+
created_at: '1321313',
572+
};
573+
574+
_customer.isUser = true;
575+
await _customer.save();
576+
577+
const customer = await Customers.updateMessengerCustomer({
578+
_id: _customer._id,
579+
doc: {
580+
email,
581+
phone,
582+
isUser: true,
583+
deviceToken,
584+
},
585+
customData,
586+
});
587+
588+
expect(customer.primaryEmail).toBe(email);
589+
expect(customer.emails).toContain(email);
590+
expect(customer.deviceTokens).toContain(deviceToken);
591+
592+
expect(customer.primaryPhone).toBe(phone);
593+
expect(customer.phones).toContain(phone);
594+
595+
expect(customer.isUser).toBe(true);
596+
expect(customer.lastName).toBe(lastName);
597+
expect(customer.description).toBe(bio);
598+
599+
const messengerData = customer.messengerData;
600+
601+
if (!messengerData) {
602+
throw new Error('messengerData is null');
603+
}
604+
605+
expect(messengerData.customData.first_name).toBeUndefined();
606+
expect(messengerData.customData.last_name).toBeUndefined();
607+
expect(messengerData.customData.bio).toBeUndefined();
608+
609+
// Do not replace previous non empty value with empty value
610+
const customerWithCustomData = await customerFactory({
611+
messengerData: { customData: { organization: 'test' } },
612+
});
613+
614+
const updated = await Customers.updateMessengerCustomer({
615+
_id: customerWithCustomData._id,
616+
doc: {},
617+
customData: {},
618+
});
619+
620+
if (!updated.messengerData) {
621+
throw new Error('messengerData is null');
622+
}
623+
624+
expect(updated.messengerData.customData.organization).toBe('test');
625+
});
626+
627+
test('getWidgetCustomer()', async () => {
628+
// emails, primaryEmail ==============
629+
let customer: ICustomerDocument | null = await customerFactory({
630+
primaryEmail: 'customer@gmail.com',
631+
emails: ['main@gmail.com'],
632+
});
633+
634+
let foundCustomer = await Customers.getWidgetCustomer({
635+
email: 'customer@gmail.com',
636+
});
637+
638+
expect(foundCustomer && foundCustomer._id).toBe(customer._id);
639+
640+
// phones
641+
customer = await customerFactory({
642+
phones: ['911111'],
643+
});
644+
645+
foundCustomer = await Customers.getWidgetCustomer({
646+
phone: '911111',
647+
});
648+
649+
expect(foundCustomer && foundCustomer._id).toBe(customer._id);
650+
651+
// primaryPhone
652+
customer = await customerFactory({
653+
primaryPhone: '24244242',
654+
});
655+
656+
foundCustomer = await Customers.getWidgetCustomer({
657+
phone: '24244242',
658+
});
659+
660+
expect(foundCustomer && foundCustomer._id).toBe(customer._id);
661+
662+
// code
663+
customer = await customerFactory({
664+
code: '24244242',
665+
});
666+
667+
foundCustomer = await Customers.getWidgetCustomer({
668+
code: '24244242',
669+
});
670+
671+
expect(foundCustomer && foundCustomer._id).toBe(customer._id);
672+
673+
// cached customer id
674+
foundCustomer = await Customers.getWidgetCustomer({
675+
cachedCustomerId: customer._id,
676+
});
677+
678+
expect(foundCustomer && foundCustomer._id).toBe(customer._id);
679+
});
680+
681+
test('updateMessengerSession()', async () => {
682+
try {
683+
await Customers.updateMessengerSession('_id', '/career/open');
684+
} catch (e) {
685+
expect(e.message).toBe('Customer not found');
686+
}
687+
688+
const now = new Date();
689+
690+
const customer = await Customers.updateMessengerSession(_customer._id, '/career/open');
691+
692+
const { messengerData } = customer;
693+
694+
expect(messengerData).toBeDefined();
695+
696+
if (messengerData) {
697+
expect(messengerData.isActive).toBeTruthy();
698+
expect(messengerData.lastSeenAt && messengerData.lastSeenAt >= now.getTime()).toBeTruthy();
699+
}
700+
701+
expect(customer.urlVisits['/career/open']).toBe(1);
702+
});
703+
704+
test('saveVisitorContactInfo()', async () => {
705+
// email ==========
706+
let customer = await Customers.saveVisitorContactInfo({
707+
customerId: _customer._id,
708+
type: 'email',
709+
value: 'test@gmail.com',
710+
});
711+
712+
let visitorContactInfo: any = customer.visitorContactInfo || {};
713+
714+
expect(visitorContactInfo.email).toBe('test@gmail.com');
715+
716+
// phone ===============
717+
customer = await Customers.saveVisitorContactInfo({
718+
customerId: _customer._id,
719+
type: 'phone',
720+
value: '985435353',
721+
});
722+
723+
visitorContactInfo = customer.visitorContactInfo || {};
724+
725+
// check company in companyIds
726+
expect(visitorContactInfo.phone).toBe('985435353');
727+
});
728+
729+
test('updateLocation()', async () => {
730+
const updated = await Customers.updateLocation(_customer._id, {
731+
language: 'en',
732+
});
733+
734+
expect(updated.location && updated.location.language).toBe('en');
735+
});
495736
});

0 commit comments

Comments
 (0)