@@ -20,6 +20,7 @@ import {
2020} from '../db/models' ;
2121import { ACTIVITY_CONTENT_TYPES , STATUSES } from '../db/models/definitions/constants' ;
2222
23+ import { ICustomerDocument } from '../db/models/definitions/customers' ;
2324import './setup.ts' ;
2425
2526describe ( '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