From f36e617e9e534a4b05f2029724d678bbd6c655b3 Mon Sep 17 00:00:00 2001
From: Tomas <tomasysh@gmail.com>
Date: 星期三, 22 十二月 2021 11:38:54 +0800
Subject: [PATCH] refactor: separate api/consultant.ts into serveral services

---
 PAMapp/store/index.ts |  103 +++++++++++++++++++++++++++++++++------------------
 1 files changed, 67 insertions(+), 36 deletions(-)

diff --git a/PAMapp/store/index.ts b/PAMapp/store/index.ts
index a117eb2..73ab54c 100644
--- a/PAMapp/store/index.ts
+++ b/PAMapp/store/index.ts
@@ -1,44 +1,65 @@
+import { StrictQueryParams } from '~/shared/models/strict-query.model';
 import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'
-import { ClientInfo, getMyAppointmentList, getMyReviewLog } from '~/assets/ts/api/appointment';
-// import * as consultant from '~/assets/ts/api/consultant';
-import { recommend, AgentOfStrictQuery, getFavoriteConsultant, addFavoriteConsultant, deleteConsultant, strictQuery } from '~/assets/ts/api/consultant';
-import { Consultants } from '~/assets/ts/models/consultant.model';
-import { isLogin } from '~/assets/ts/auth';
-import { AppointmentLog } from '~/assets/ts/models/appointment.model';
-import { getFavoriteFromStorage, setFavoriteToStorage } from '~/assets/ts/storageConsultant';
 
+import { getMyReviewLog } from '~/shared/api/appointment';
+import { getFavoriteFromStorage, setFavoriteToStorage } from '~/shared/storageConsultant';
+
+import myConsultantService from '~/shared/services/my-consultant.service';
+import queryConsultantService from '~/shared/services/query-consultant.service';
+import appointmentService from '~/shared/services/appointment.service';
+
+import { Consultant } from '~/shared/models/consultant.model';
+import { AppointmentLog } from '~/shared/models/appointment.model';
+import { ClientInfo } from '~/shared/models/client.model';
+import { AgentOfStrictQuery } from '~/shared/models/strict-query.model';
 @Module
 export default class Store extends VuexModule {
-    recommendList: Consultants[] = [];
+    recommendList: Consultant[] = [];
     strictQueryList: AgentOfStrictQuery[] = [];
-    myConsultantList: Consultants[] = [];
+    myConsultantList: Consultant[] = [];
 
     myAppointmentList: ClientInfo[] = [];
+    myNewAppointmentSum: number = 0;
 
     myAppointmentReviewLogList: AppointmentLog[] = [];
 
-    @Mutation updateRecommend(data: Consultants[]) {
+    get isUserLogin() {
+        return this.context.getters['localStorage/isUserLogin'];
+    }
+
+    @Mutation
+    updateRecommend(data: Consultant[]) {
         this.recommendList = data;
     }
 
-    @Mutation updateConsultantList(data: Consultants[]) {
+    @Mutation
+    updateConsultantList(data: Consultant[]) {
         this.myConsultantList = data;
     }
 
-    @Mutation updateStrictQueryList(data: AgentOfStrictQuery[]) {
+    @Mutation
+    updateStrictQueryList(data: AgentOfStrictQuery[]) {
         this.strictQueryList = data;
     }
 
-    @Mutation updateMyAppointmentList(data: ClientInfo[]) {
+    @Mutation
+    updateMyAppointmentList(data: ClientInfo[]) {
         this.myAppointmentList = data;
     }
 
-    @Mutation updateMyAppointmentReviewLog(data: AppointmentLog[]) {
+    @Mutation
+    updateMyNewAppointmentSum(newAppointmentSum: number) {
+      this.myNewAppointmentSum = newAppointmentSum;
+    }
+
+    @Mutation
+    updateMyAppointmentReviewLog(data: AppointmentLog[]) {
         this.myAppointmentReviewLogList = data;
     }
 
-    @Action storeRecommendList() {
-        recommend().then(data => {
+    @Action
+    storeRecommendList() {
+        queryConsultantService.getRecommendConsultantList().then(data => {
             this.context.commit('updateRecommend', data)
         })
     }
@@ -46,7 +67,8 @@
     @Action
     async storeConsultantList() {
         const localData = getFavoriteFromStorage();
-        if (!isLogin()) {
+
+        if (!this.isUserLogin) {
             this.context.commit('updateConsultantList', localData)
             return;
         };
@@ -54,13 +76,13 @@
 
         if (localData?.length) {
             const agentNoList = localData.map(i => i.agentNo)
-            await addFavoriteConsultant(agentNoList).then(res => {
+            await queryConsultantService.addFavoriteConsultant(agentNoList).then(res => {
                 localStorage.removeItem('favoriteConsultant')
             })
         }
 
-        getFavoriteConsultant().then(data => {
-            this.context.commit('updateConsultantList', data)
+        myConsultantService.getFavoriteConsultantList().then(data => {
+            this.context.commit('updateConsultantList', data);
         })
     }
 
@@ -71,10 +93,10 @@
         // no agent was removed
         if (left.length === this.myConsultantList.length) return false;
 
-        if (!isLogin()) {
+        if (!this.isUserLogin) {
             setFavoriteToStorage(left);
         } else {
-            await deleteConsultant(agentNo)
+            await myConsultantService.deleteConsultant(agentNo)
         }
 
         this.context.commit('updateConsultantList', left)
@@ -83,14 +105,13 @@
     }
 
     @Action
-    async addToMyConsultantList(consultantToAdd: Consultants) {
+    async addToMyConsultantList(consultantToAdd: Consultant) {
         if (consultantToAdd) {
             const found = this.myConsultantList.find(item => item.agentNo === consultantToAdd.agentNo);
             if (!found) {
                 const newData = [consultantToAdd].concat(this.myConsultantList);
-
-                if (isLogin()) {
-                    await addFavoriteConsultant([consultantToAdd.agentNo])
+                if (this.isUserLogin) {
+                    await queryConsultantService.addFavoriteConsultant([consultantToAdd.agentNo])
                 } else {
                     setFavoriteToStorage(newData);
                 }
@@ -105,32 +126,42 @@
     }
 
     @Action
-    storeMyAppointmentList() {
-        getMyAppointmentList().then((data) => {
-            this.context.commit('updateMyAppointmentList', data)
+    storeMyAppointmentList(): void {
+      appointmentService.getMyAppointmentList().then((data) => {
+            const newAppointmentSum = data.filter(item => !item.consultantViewTime || item.consultantViewTime === null).length;
+            this.context.commit('updateMyAppointmentList', data);
+            this.context.commit('updateMyNewAppointmentSum', newAppointmentSum);
         });
     }
 
     @Action
     storeMyAppointmentReviewLog() {
         getMyReviewLog().then((data) => {
-            this.context.commit('updateMyAppointmentReviewLog', data);
+            const dataWithLatestDate = data.map((item) => {
+                return {
+                    ...item,
+                    compareDate: new Date(item.lastModifiedDate)
+                }
+            });
+            const sortedData = dataWithLatestDate.sort((a, b) => +b.compareDate - +a.compareDate);
+            this.context.commit('updateMyAppointmentReviewLog', sortedData);
         });
     }
 
-    @Action updateMyAppointment(myAppointment: ClientInfo) {
+    @Action
+    updateMyAppointment(myAppointment: ClientInfo) {
         const data = this.myAppointmentList.filter(item => item.id !== myAppointment.id);
         data.unshift(myAppointment);
         this.context.commit('updateMyAppointmentList', data)
     }
 
     @Action
-    async storeStrictQueryList(strictQueryDto) {
-        return await strictQuery(strictQueryDto).then(res=>{
+    async storeStrictQueryList(strictQueryDto: StrictQueryParams) {
+        return await queryConsultantService.strictQuery(strictQueryDto).then(res=>{
             this.context.commit('localStorage/storageRecommendConsultant', JSON.stringify(strictQueryDto));
-            this.context.commit('updateStrictQueryList', res.data)
-            return res.data.length;
+            this.context.commit('updateStrictQueryList', res)
+            return res.length;
         });
     }
 
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3