Advertisement
Guest User

Untitled

a guest
Mar 8th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.   AppointmentForGet,
  3.   ClinicAppointmentForUpdate,
  4.   ClinicsService,
  5.   PagedPatients,
  6.   Patient,
  7.   PatientsService,
  8.   UserAndRole,
  9.   UserRead,
  10.   UsersService
  11. } from '../../../out';
  12. import {PopupDiagComponent} from '../appointments/calendar/popup-diag/popup-diag.component';
  13. import {CalendarEvent, CalendarEventTimesChangedEvent} from 'angular-calendar';
  14. import {AuthenticationService} from './authentication.service';
  15. import {ErrorHandlingService} from './error-handling.service';
  16. import {isSameDay, isSameMonth} from 'date-fns';
  17. import {LoaderService} from './loader.service';
  18. import {ChartService} from './chart.service';
  19. import {Injectable} from '@angular/core';
  20. import {SmsService} from './sms.service';
  21. import {Router} from '@angular/router';
  22. import swal from 'sweetalert2';
  23. import {Subject} from 'rxjs';
  24.  
  25. const colors: any = [
  26.   {
  27.     primary: '#abfaf9',
  28.     secondary: '#abfaf9'
  29.   },
  30.   {
  31.     primary: '#faabbb',
  32.     secondary: '#faabbb'
  33.   },
  34.   {
  35.     primary: '#75fea5',
  36.     secondary: '#75fea5'
  37.   }
  38. ];
  39.  
  40.  
  41. @Injectable({
  42.   providedIn: 'root',
  43. })
  44.  
  45. export class AppointmentsService {
  46.  
  47.   modalData: {
  48.     action: string;
  49.     event: CalendarEvent;
  50.   };
  51.   updateAppointment: ClinicAppointmentForUpdate = {} as ClinicAppointmentForUpdate;
  52.   monthNames = ['January', 'February', 'March', 'April', 'May', 'June',
  53.     'July', 'August', 'September', 'October', 'November', 'December'];
  54.   patientCollections: PagedPatients;
  55.   appointments: Array<AppointmentForGet> = [];
  56.   userCollections: Array<UserRead> = [];
  57.   refresh: Subject<any> = new Subject();
  58.   lazyLoadingArray: Array<any> = [];
  59.   data: any = [{id: '', text: ''}];
  60.   editingAppId: string | number;
  61.   addedDate?: Date = new Date();
  62.   appointmentPatientId: string;
  63.   events: CalendarEvent[] = [];
  64.   viewDate: Date = new Date();
  65.   users: Array<UserAndRole>;
  66.   lazyLoadingIndex: number;
  67.   appTitleService: string;
  68.   activeDayIsOpen = true;
  69.   AppServiceTime?: Date;
  70.   MS_PER_MINUTE = 60000;
  71.   monthShowed: string;
  72.   userFilter = false;
  73.   yearShowed: number;
  74.   startDate: string;
  75.   clinicId: string;
  76.   endDate: string;
  77.   view = 'month';
  78.   userId: string;
  79.   interval: any;
  80.  
  81.   constructor(private auth: AuthenticationService,
  82.               private route: Router,
  83.               private api: ClinicsService,
  84.               private api2: PatientsService,
  85.               private smsService: SmsService,
  86.               private api3: UsersService,
  87.               private loaderService: LoaderService,
  88.               private errHandler: ErrorHandlingService,
  89.               public popup: PopupDiagComponent,
  90.               private chartService: ChartService) {
  91.     this.clinicId = auth.getClinicId();
  92.     this.userId = auth.getId();
  93.     this.getClinicUsers();
  94.   }
  95.  
  96.   AddedValueToTime(d: Date, minutes: number): any {
  97.     this.addedDate = new Date(d.getTime() + minutes * this.MS_PER_MINUTE);
  98.     d = this.addedDate;
  99.     return d;
  100.   }
  101.  
  102.   getDate(event, userId?) {
  103.     if (this.monthShowed !== this.monthNames[event.getMonth()] || userId) {
  104.       this.loaderService.show();
  105.       this.monthShowed = this.monthNames[event.getMonth()];
  106.       this.yearShowed = event.getFullYear();
  107.  
  108.       const a = event.getMonth() + 1 + '' + this.yearShowed;
  109.       this.lazyLoadingIndex = Number(a);
  110.  
  111.       const startDateString = this.monthShowed + ' ' + 1 + ', ' + this.yearShowed;
  112.       const endDateString = this.monthShowed + ' ' +
  113.         (this.getDaysInMonth(Number(event.getMonth()), this.yearShowed)) +
  114.         ', ' + this.yearShowed;
  115.       this.startDate = new Date(startDateString).toISOString();
  116.       this.endDate = new Date(endDateString).toISOString();
  117.       if (!this.lazyLoadingArray[this.lazyLoadingIndex] || userId) {
  118.         this.fetchClinicAppointments(undefined, userId);
  119.       } else {
  120.         this.events = [];
  121.         this.auth.refreshToken('appointments.service, getDate').then(() => {
  122.           this.fetchAppEvent(this.lazyLoadingArray[this.lazyLoadingIndex]).then(() => {
  123.             setTimeout(() => {
  124.               this.refresh.next();
  125.             }, 1000);
  126.           });
  127.         });
  128.       }
  129.     }
  130.   }
  131.  
  132.   getDaysInMonth(month, year) {
  133.     return new Date(year, month + 1, 0).getDate();
  134.   }
  135.  
  136.   fetchClinicAppointments(searchString?, userId?) {
  137.     this.loaderService.show();
  138.     this.refresh.next();
  139.     if (searchString && searchString !== '') {
  140.       const currentDate = new Date(Date.now());
  141.       const year = currentDate.getFullYear() + 1;
  142.       this.monthShowed = this.monthNames[currentDate.getMonth()];
  143.       this.yearShowed = currentDate.getFullYear();
  144.       const startDateString = this.monthShowed + ' ' + 1 + ', ' + this.yearShowed;
  145.       const endDateString = this.monthShowed + ' ' +
  146.         (this.getDaysInMonth(Number(this.viewDate.getMonth()), year)) +
  147.         ', ' + year;
  148.       this.auth.refreshToken('appointments.service fetchClinicAppointments if').then(() => {
  149.         if (userId) {
  150.           userId = this.userId;
  151.           if (this.userFilter) {
  152.             this.userFilter = false;
  153.             userId = undefined;
  154.           } else {
  155.             this.userFilter = true;
  156.           }
  157.         }
  158.         this.api.getClinicItemAppointmentsCollection(this.clinicId, new Date(endDateString).toISOString(),
  159.           new Date(startDateString).toISOString(), searchString, userId).subscribe((res) => {
  160.           if (res && res.length === 0) {
  161.             this.loaderService.hide();
  162.             this.refresh.next();
  163.             swal('Appointments Search Failed', 'Nothing found, Please try something else', 'info');
  164.           } else {
  165.             const interv = setInterval(() => {
  166.               if (res.length === this.events.length || this.events.length > res.length) {
  167.                 clearInterval(interv);
  168.                 this.loaderService.hide();
  169.               }
  170.             }, 1500);
  171.             for (let i = 0; i < res.length; i++) {
  172.               if (!res[i].summary) {
  173.                 res[i].summary = '';
  174.               }
  175.               if (res[i].patient_id) {
  176.                 this.api2.getPatientItem(res[i].patient_id).subscribe((res2) => {
  177.                   res[i] = Object.assign(res2, res[i]);
  178.                 }, err => {
  179.                   this.errHandler.handleError(err, 'AppointmentsService, getPatientItem');
  180.                 });
  181.               }
  182.               if (i + 1 === res.length) {
  183.                 this.popup.openDialog();
  184.                 this.chartService.searchAppointments = res;
  185.                 this.popup.searchAppointments = res;
  186.               }
  187.             }
  188.           }
  189.         }, err => {
  190.           this.errHandler.handleError(err, 'AppointmentsService, getClinicItemAppointmentsCollection');
  191.         });
  192.       });
  193.     } else {
  194.       this.auth.refreshToken('appointments.service fetchClinicAppointments else').then(() => {
  195.         this.monthShowed = this.monthNames[this.viewDate.getMonth()];
  196.         this.yearShowed = this.viewDate.getFullYear();
  197.  
  198.         const a = this.viewDate.getMonth() + 1 + '' + this.yearShowed;
  199.         this.lazyLoadingIndex = Number(a);
  200.  
  201.         const startDateString = this.monthShowed + ' ' + 1 + ', ' + this.yearShowed;
  202.         const endDateString = this.monthShowed + ' ' +
  203.           (this.getDaysInMonth(Number(this.viewDate.getMonth()), this.yearShowed)) +
  204.           ', ' + this.yearShowed;
  205.         this.startDate = new Date(startDateString).toISOString();
  206.         this.endDate = new Date(endDateString).toISOString();
  207.         this.events = [];
  208.         if (userId) {
  209.           userId = this.userId;
  210.           if (this.userFilter) {
  211.             this.userFilter = false;
  212.             userId = undefined;
  213.           } else {
  214.             this.userFilter = true;
  215.           }
  216.         }
  217.         this.api.getClinicItemAppointmentsCollection(this.clinicId, this.endDate.toString(),
  218.           this.startDate.toString(), undefined, userId).subscribe((res) => {
  219.           if (res && res.length === 0) {
  220.             this.loaderService.hide();
  221.             this.refresh.next();
  222.           } else {
  223.             const interv = setInterval(() => {
  224.               if (res.length === this.events.length || this.events.length > res.length) {
  225.                 clearInterval(interv);
  226.                 this.loaderService.hide();
  227.               }
  228.             }, 1500);
  229.             this.lazyLoadingArray[this.lazyLoadingIndex] = res;
  230.             this.appointments = res;
  231.             this.auth.refreshToken('appointments.service, fetchClinicAppointments() else else').then(() => {
  232.               this.fetchAppEvent(res).then(() => {
  233.                 setTimeout(() => {
  234.                   this.refresh.next();
  235.                 }, 1000);
  236.               });
  237.             });
  238.           }
  239.         }, err => {
  240.           this.errHandler.handleError(err, 'AppointmentsService, getClinicItemAppointmentsCollection');
  241.         });
  242.       });
  243.     }
  244.   }
  245.  
  246.   fetchAppEvent(res, index?) {
  247.     this.loaderService.show();
  248.     this.refresh.next();
  249.     return new Promise((resolve, reject) => {
  250.       if (res && res.length === 0) {
  251.         this.refresh.next();
  252.         reject(res.length);
  253.       } else {
  254.         const interv = setInterval(() => {
  255.           if (res.length === this.events.length || this.events.length > res.length) {
  256.             clearInterval(interv);
  257.             this.loaderService.hide();
  258.           }
  259.         }, 1500);
  260.         for (let i = 0; i < res.length; i++) {
  261.           if (!res[i].is_clinic_appointment) {
  262.             if (res[i].patient_id) {
  263.               this.getPatient(res[i].patient_id).then((patient: Patient) => {
  264.                 if (patient) {
  265.                   if (patient.first_name && patient.last_name) {
  266.                     let titleText: string;
  267.                     if (res[i].summary) {
  268.                       titleText = patient.last_name + ' ' + patient.first_name +
  269.                         ', ' + res[i].summary;
  270.                     } else {
  271.                       titleText = patient.last_name + ' ' + patient.first_name;
  272.                     }
  273.                     if (res[i].user_participants.length > 0) {
  274.                       for (let y = 0; y < res[i].user_participants.length; y++) {
  275.                         this.getUserCollection(res[i].user_participants[y]).then((userCollection) => {
  276.                           const cid = 0;
  277.                           const appEvent = {
  278.                             id: res[i].id,
  279.                             title: titleText,
  280.                             start: new Date(res[i].start_time),
  281.                             end: new Date(res[i].stop_time),
  282.                             color: colors[cid],
  283.                             draggable: true,
  284.                             resizable: {
  285.                               beforeStart: true,
  286.                               afterEnd: true
  287.                             },
  288.                             meta: {
  289.                               user: {
  290.                                 id: res[i].user_participants[y],
  291.                                 // @ts-ignore
  292.                                 name: userCollection.first_name + ' ' + userCollection.last_name,
  293.                                 color: colors[cid]
  294.                               }
  295.                             }
  296.                           };
  297.                           this.appointments.sort((a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime());
  298.                           appEvent.title = titleText + ' by ' + userCollection.first_name + ' ' + userCollection.last_name;
  299.                           if (index || index === 0) {
  300.                             this.events[index] = appEvent;
  301.                           } else {
  302.                             this.events.push(appEvent);
  303.                           }
  304.                           this.refresh.next();
  305.                           if (i + 1 === res.length) {
  306.                             resolve('done');
  307.                           }
  308.                         });
  309.                       }
  310.                     } else {
  311.                       const cid = 0;
  312.                       const appEvent = {
  313.                         id: res[i].id,
  314.                         title: titleText,
  315.                         start: new Date(res[i].start_time),
  316.                         end: new Date(res[i].stop_time),
  317.                         color: colors[0],
  318.                         draggable: true,
  319.                         resizable: {
  320.                           beforeStart: true,
  321.                           afterEnd: true
  322.                         },
  323.                         meta: {
  324.                           user: {
  325.                             id: 0,
  326.                             name: 'All',
  327.                             color: colors[cid]
  328.                           }
  329.                         }
  330.                       };
  331.                       this.appointments.sort((a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime());
  332.                       if (index || index === 0) {
  333.                         this.events[index] = appEvent;
  334.                       } else {
  335.                         this.events.push(appEvent);
  336.                       }
  337.                       this.refresh.next();
  338.                       if (i + 1 === res.length) {
  339.                         resolve('done');
  340.                       }
  341.                     }
  342.                   }
  343.                 }
  344.               }).catch(() => {
  345.                 let titleText: string;
  346.                 if (res[i].summary) {
  347.                   titleText = 'Patient not found: ' + res[i].summary;
  348.                 } else {
  349.                   titleText = 'Patient not found';
  350.                 }
  351.                 if (res[i].user_participants.length > 0) {
  352.                   for (let y = 0; y < res[i].user_participants.length; y++) {
  353.                     this.getUserCollection(res[i].user_participants[y]).then((userCollection) => {
  354.                       const cid = 0;
  355.                       const appEvent = {
  356.                         id: res[i].id,
  357.                         title: titleText,
  358.                         start: new Date(res[i].start_time),
  359.                         end: new Date(res[i].stop_time),
  360.                         color: colors[cid],
  361.                         draggable: true,
  362.                         resizable: {
  363.                           beforeStart: true,
  364.                           afterEnd: true
  365.                         },
  366.                         meta: {
  367.                           user: {
  368.                             id: res[i].user_participants[y],
  369.                             // @ts-ignore
  370.                             name: userCollection.first_name + ' ' + userCollection.last_name,
  371.                             color: colors[cid]
  372.                           }
  373.                         }
  374.                       };
  375.                       this.appointments.sort((a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime());
  376.                       appEvent.title = titleText + ' by ' + userCollection.first_name + ' ' + userCollection.last_name;
  377.                       if (index || index === 0) {
  378.                         this.events[index] = appEvent;
  379.                       } else {
  380.                         this.events.push(appEvent);
  381.                       }
  382.                       this.refresh.next();
  383.                     });
  384.                     if (i + 1 === res.length) {
  385.                       resolve('done');
  386.                     }
  387.                   }
  388.                 } else {
  389.                   const cid = 0;
  390.                   const appEvent = {
  391.                     id: res[i].id,
  392.                     title: titleText,
  393.                     start: new Date(res[i].start_time),
  394.                     end: new Date(res[i].stop_time),
  395.                     color: colors[cid],
  396.                     draggable: true,
  397.                     resizable: {
  398.                       beforeStart: true,
  399.                       afterEnd: true
  400.                     },
  401.                     meta: {
  402.                       user: {
  403.                         id: 0,
  404.                         name: 'All',
  405.                         color: colors[cid]
  406.                       }
  407.                     }
  408.                   };
  409.                   this.appointments.sort((a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime());
  410.                   if (index || index === 0) {
  411.                     this.events[index] = appEvent;
  412.                   } else {
  413.                     this.events.push(appEvent);
  414.                   }
  415.                   this.refresh.next();
  416.                   if (i + 1 === res.length) {
  417.                     resolve('done');
  418.                   }
  419.                 }
  420.               });
  421.             } else {
  422.               let titleText: string;
  423.               if (res[i].summary) {
  424.                 titleText = 'New Patient: ' + res[i].summary;
  425.               } else {
  426.                 titleText = 'New Patient';
  427.               }
  428.               if (res[i].user_participants.length > 0) {
  429.                 for (let y = 0; y < res[i].user_participants.length; y++) {
  430.                   this.getUserCollection(res[i].user_participants[y]).then((userCollection) => {
  431.                     const cid = 1;
  432.                     const appEvent = {
  433.                       id: res[i].id,
  434.                       title: titleText,
  435.                       start: new Date(res[i].start_time),
  436.                       end: new Date(res[i].stop_time),
  437.                       color: colors[cid],
  438.                       draggable: true,
  439.                       resizable: {
  440.                         beforeStart: true,
  441.                         afterEnd: true
  442.                       },
  443.                       meta: {
  444.                         user: {
  445.                           id: res[i].user_participants[y],
  446.                           // @ts-ignore
  447.                           name: userCollection.first_name + ' ' + userCollection.last_name,
  448.                           color: colors[cid]
  449.                         }
  450.                       }
  451.                     };
  452.                     this.appointments.sort((a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime());
  453.                     appEvent.title = titleText + ' by ' + userCollection.first_name + ' ' + userCollection.last_name;
  454.                     if (index || index === 0) {
  455.                       this.events[index] = appEvent;
  456.                     } else {
  457.                       this.events.push(appEvent);
  458.                     }
  459.                     this.refresh.next();
  460.                   });
  461.                   if (i + 1 === res.length) {
  462.                     resolve('done');
  463.                   }
  464.                 }
  465.               } else {
  466.                 const cid = 1;
  467.                 const appEvent = {
  468.                   id: res[i].id,
  469.                   title: titleText,
  470.                   start: new Date(res[i].start_time),
  471.                   end: new Date(res[i].stop_time),
  472.                   color: colors[cid],
  473.                   draggable: true,
  474.                   resizable: {
  475.                     beforeStart: true,
  476.                     afterEnd: true
  477.                   },
  478.                   meta: {
  479.                     user: {
  480.                       id: 0,
  481.                       name: 'All',
  482.                       color: colors[cid]
  483.                     }
  484.                   }
  485.                 };
  486.                 this.appointments.sort((a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime());
  487.                 if (index || index === 0) {
  488.                   this.events[index] = appEvent;
  489.                 } else {
  490.                   this.events.push(appEvent);
  491.                 }
  492.                 this.refresh.next();
  493.                 if (i + 1 === res.length) {
  494.                   resolve('done');
  495.                 }
  496.               }
  497.             }
  498.           } else {
  499.             let titleText: string;
  500.             if (res[i].summary) {
  501.               titleText = 'Clinic Appointment: ' + res[i].summary;
  502.             } else {
  503.               titleText = 'Clinic Appointment';
  504.             }
  505.             if (res[i].user_participants.length > 0) {
  506.               for (let y = 0; y < res[i].user_participants.length; y++) {
  507.                 this.getUserCollection(res[i].user_participants[y]).then((userCollection) => {
  508.                   const cid = 2;
  509.                   const appEvent = {
  510.                     id: res[i].id,
  511.                     title: titleText,
  512.                     start: new Date(res[i].start_time),
  513.                     end: new Date(res[i].stop_time),
  514.                     color: colors[cid],
  515.                     draggable: true,
  516.                     resizable: {
  517.                       beforeStart: true,
  518.                       afterEnd: true
  519.                     },
  520.                     meta: {
  521.                       user: {
  522.                         id: res[i].user_participants[y],
  523.                         // @ts-ignore
  524.                         name: userCollection.first_name + ' ' + userCollection.last_name,
  525.                         color: colors[cid]
  526.                       }
  527.                     }
  528.                   };
  529.                   this.appointments.sort((a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime());
  530.                   appEvent.title = titleText + ' by ' + userCollection.first_name + ' ' + userCollection.last_name;
  531.                   if (index || index === 0) {
  532.                     this.events[index] = appEvent;
  533.                   } else {
  534.                     this.events.push(appEvent);
  535.                   }
  536.                   this.refresh.next();
  537.                 });
  538.                 if (i + 1 === res.length) {
  539.                   resolve('done');
  540.                 }
  541.               }
  542.             } else {
  543.               const cid = 2;
  544.               const appEvent = {
  545.                 id: res[i].id,
  546.                 title: titleText,
  547.                 start: new Date(res[i].start_time),
  548.                 end: new Date(res[i].stop_time),
  549.                 color: colors[cid],
  550.                 draggable: true,
  551.                 resizable: {
  552.                   beforeStart: true,
  553.                   afterEnd: true
  554.                 },
  555.                 meta: {
  556.                   user: {
  557.                     id: 0,
  558.                     name: 'All',
  559.                     color: colors[cid]
  560.                   }
  561.                 }
  562.               };
  563.               this.new.sort((a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime());
  564.               if (index || index === 0) {
  565.                 this.events[index] = appEvent;
  566.               } else {
  567.                 this.events.push(appEvent);
  568.               }
  569.               this.refresh.next();
  570.               if (i + 1 === res.length) {
  571.                 resolve('done');
  572.               }
  573.             }
  574.           }
  575.         }
  576.       }
  577.     });
  578.   }
  579.  
  580.   getClinicUsers() {
  581.     this.api.getClinicItemWithRolesCollection(this.clinicId).subscribe((res) => {
  582.       this.users = res;
  583.       this.getUsersCollection();
  584.     }, err => {
  585.       this.errHandler.handleError(err, 'AppointmentsService, getClinicItemWithRolesCollection');
  586.     });
  587.   }
  588.  
  589.   getUsersCollection() {
  590.     this.userCollections = [];
  591.     for (let i = 0; i < this.users.length; i++) {
  592.       this.api3.getUserItem(this.clinicId, this.users[i].user_id).subscribe((res) => {
  593.         this.userCollections.push(res);
  594.         if (i + 1 === this.users.length) {
  595.           this.data = this.getDataEnum(this.userCollections);
  596.         }
  597.       }, err => {
  598.         // this.errHandler.handleError(err, 'AppointmentsService, getUserItem');
  599.       });
  600.     }
  601.   }
  602.  
  603.   getUserCollection(id: string) {
  604.     return new Promise((resolve, reject) => {
  605.       this.api3.getUserItem(this.clinicId, id).subscribe((res) => {
  606.         resolve(res);
  607.       }, err => {
  608.         // this.errHandler.handleError(err, 'AppointmentsService, getUserItem');
  609.         reject();
  610.       });
  611.     });
  612.   }
  613.  
  614.   getPatient(id: string) {
  615.     return new Promise(((resolve, reject) => {
  616.       this.api2.getPatientItem(id).subscribe((res) => {
  617.         for (const one in res) {
  618.           if (res[one] === null) {
  619.             res[one] = '';
  620.           }
  621.         }
  622.         // @ts-ignore
  623.         if (res.gender === 'Female') {
  624.           res.gender = Patient.GenderEnum.Female;
  625.         } else {
  626.           // @ts-ignore
  627.           if (res.gender === 'Male') {
  628.             res.gender = Patient.GenderEnum.Male;
  629.           }
  630.           resolve(res);
  631.         }
  632.       }, err => {
  633.         reject('error');
  634.       });
  635.     }));
  636.   }
  637.  
  638.   getDataEnum(user: any) {
  639.     const arr = [];
  640.     for (const s of user) {
  641.       if (s.middle_name && s.first_name && s.last_name) {
  642.         if (s.mobile_phone) {
  643.           if (s.email) {
  644.             arr.push({id: s.id, text: s.first_name + ' ' + s.middle_name + ' ' + s.last_name + ' || ' + s.mobile_phone + ' || ' + s.email});
  645.           } else {
  646.             arr.push({id: s.id, text: s.first_name + ' ' + s.middle_name + ' ' + s.last_name + ' || ' + s.mobile_phone});
  647.           }
  648.         } else if (s.phone) {
  649.           if (s.email) {
  650.             arr.push({id: s.id, text: s.first_name + ' ' + s.middle_name + ' ' + s.last_name + ' || ' + s.phone + ' || ' + s.email});
  651.           } else {
  652.             arr.push({id: s.id, text: s.first_name + ' ' + s.middle_name + ' ' + s.last_name + ' || ' + s.phone});
  653.           }
  654.         } else {
  655.           if (s.email) {
  656.             arr.push({id: s.id, text: s.first_name + ' ' + s.middle_name + ' ' + s.last_name + ' || ' + s.email});
  657.           } else {
  658.             arr.push({id: s.id, text: s.first_name + ' ' + s.middle_name + ' ' + s.last_name});
  659.           }
  660.         }
  661.       } else {
  662.         if (s.mobile_phone) {
  663.           if (s.email) {
  664.             arr.push({id: s.id, text: s.first_name + ' ' + s.last_name + ' || ' + s.mobile_phone + ' || ' + s.email});
  665.           } else {
  666.             arr.push({id: s.id, text: s.first_name + ' ' + s.last_name + ' || ' + s.mobile_phone});
  667.           }
  668.         } else if (s.phone) {
  669.           if (s.email) {
  670.             arr.push({id: s.id, text: s.first_name + ' ' + s.last_name + ' || ' + s.phone + ' || ' + s.email});
  671.           } else {
  672.             arr.push({id: s.id, text: s.first_name + ' ' + s.last_name + ' || ' + s.phone});
  673.           }
  674.         } else {
  675.           if (s.email) {
  676.             arr.push({id: s.id, text: s.first_name + ' ' + s.last_name + ' || ' + s.email});
  677.           } else {
  678.             arr.push({id: s.id, text: s.first_name + ' ' + s.last_name});
  679.           }
  680.         }
  681.       }
  682.     }
  683.     return arr;
  684.   }
  685.  
  686.   dayClicked({date}: { date: Date }): void {
  687.     if (isSameMonth(date, this.viewDate)) {
  688.       if (
  689.         (isSameDay(this.viewDate, date) && this.activeDayIsOpen === true)
  690.       ) {
  691.         this.activeDayIsOpen = false;
  692.         this.view = 'day';
  693.         this.viewDate = date;
  694.       } else {
  695.         this.activeDayIsOpen = true;
  696.         this.viewDate = date;
  697.       }
  698.     }
  699.   }
  700.  
  701.   eventTimesChanged({
  702.                       event,
  703.                       newStart,
  704.                       newEnd,
  705.                     }: CalendarEventTimesChangedEvent): void {
  706.     event.start = newStart;
  707.     event.end = newEnd;
  708.     this.updateAppointment = this.getAppointmentById(event.id);
  709.     this.updateAppointment = {
  710.       patient_id: this.updateAppointment.patient_id,
  711.       alarm_ts: new Date(this.updateAppointment.alarm_ts),
  712.       notes: this.updateAppointment.notes,
  713.       start_time: new Date(this.updateAppointment.start_time),
  714.       stop_time: new Date(this.updateAppointment.stop_time),
  715.       summary: this.updateAppointment.summary,
  716.       user_participants: this.updateAppointment.user_participants
  717.     };
  718.     this.handleEvent('Dropped or resized', event);
  719.     this.updateAppointment.start_time = new Date(newStart);
  720.     this.updateAppointment.stop_time = new Date(newEnd);
  721.     for (const i in this.updateAppointment) {
  722.       if (!this.updateAppointment[i]) {
  723.         delete this.updateAppointment[i];
  724.       }
  725.     }
  726.     this.auth.refreshToken('appointments.service eventTimesChanged').then(() => {
  727.       this.api.putClinicItemWithAppointmentItem(this.clinicId, Number(event.id), this.updateAppointment)
  728.         .subscribe((res) => {
  729.         }, err => {
  730.           this.errHandler.handleError(err, 'AppointmentsService, putClinicItemWithAppointmentItem');
  731.         });
  732.     });
  733.     this.refresh.next();
  734.   }
  735.  
  736.   userChanged({event, newUser}) {
  737.     event.color = newUser.color;
  738.     event.meta.user = newUser;
  739.     this.events = [...this.events];
  740.   }
  741.  
  742.   handleEvent(action: string, event: CalendarEvent): void {
  743.     this.modalData = {event, action};
  744.     if (action === 'Clicked') {
  745.       this.editingAppId = event.id;
  746.       this.appTitleService = event.title;
  747.       this.route.navigateByUrl('/appointments/update/' + this.editingAppId);
  748.     }
  749.   }
  750.  
  751.   hourSegmentClicked(date: Date) {
  752.     this.route.navigateByUrl('/appointments/add');
  753.     this.AppServiceTime = date;
  754.   }
  755.  
  756.   getAppointmentById(id: string | number): AppointmentForGet | null {
  757.     for (const app of this.appointments) {
  758.       if (app.id === id) {
  759.         return app;
  760.       }
  761.     }
  762.     return null;
  763.   }
  764.  
  765.   sendSms() {
  766.     const start = new Date();
  767.     start.setHours(0, 0, 0, 0);
  768.     const end = new Date();
  769.     end.setHours(23, 59, 59, 999);
  770.     this.auth.refreshToken('appointments.service sendSMS').then(() => {
  771.       this.api.getClinicItemAppointmentsCollection(this.clinicId, end.toISOString(), start.toISOString()).subscribe((res) => {
  772.         for (let i = 0; i < res.length; i++) {
  773.           if (res[i].patient_id) {
  774.             this.api2.getPatientItem(res[i].patient_id).subscribe((patientRes) => {
  775.               if (patientRes.mobile_phone) {
  776.                 let gender: string;
  777.                 if (patientRes.gender === Patient.GenderEnum.Female) {
  778.                   gender = 'Mrs. ';
  779.                 } else if (patientRes.gender === Patient.GenderEnum.Male) {
  780.                   gender = 'Mr. ';
  781.                 }
  782.                 this.smsService.sendPostRequest('Cloudclinic', patientRes.mobile_phone, 'Dear ' + gender +
  783.                   patientRes.first_name + ',\nPlease be informed that your appointment is today at:' +
  784.                   new Date(res[i].start_time).toLocaleString()).then((smsRes) => {
  785.                   if (smsRes === 'queued') {
  786.                     swal('Success', 'Sms Message Sent to patient ' + patientRes.first_name + ' as an appointment reminder!', 'success');
  787.                   } else {
  788.                     swal('Failed', 'Sms failed to sent to patient ' + patientRes.first_name + '!', 'error');
  789.                   }
  790.                 });
  791.               } else {
  792.                 swal('error', 'Patient ' + patientRes.first_name + ' has no mobile phone number registered to his records',
  793.                   'error');
  794.               }
  795.             });
  796.           }
  797.         }
  798.       }, err => {
  799.         this.errHandler.handleError(err, 'AppointmentsService, getClinicItemAppointmentsCollection');
  800.       });
  801.     });
  802.   }
  803. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement