Pārlūkot izejas kodu

refactor: ajuste no fluxo de pagamento para realizar atraves de services-packages

Gustavo Mantovani 5 dienas atpakaļ
vecāks
revīzija
e8cc27dbbb

+ 0 - 10
src/api/payment.js

@@ -1,20 +1,10 @@
 import api from 'src/api';
 
-export const paySchedule = async (scheduleId, payload) => {
-  const { data } = await api.post(`/payment/schedule/${scheduleId}/pay`, payload);
-  return data.payload;
-};
-
 export const payServicePackage = async (servicePackageId, payload) => {
   const { data } = await api.post(`/payment/service-package/${servicePackageId}/pay`, payload);
   return data.payload;
 };
 
-export const getSchedulePixPayment = async (scheduleId) => {
-  const { data } = await api.get(`/payment/schedule/${scheduleId}/pix`);
-  return data.payload;
-};
-
 export const getServicePackagePix = async (servicePackageId) => {
   const { data } = await api.get(`/payment/service-package/${servicePackageId}/pix`);
   return data.payload;

+ 9 - 28
src/components/dashboard/ScheduleAcceptedDialog.vue

@@ -153,8 +153,7 @@ import SchedulePaymentDialog from "./SchedulePaymentDialog.vue";
 import SchedulePaymentPixDialog from "./SchedulePaymentPixDialog.vue";
 
 const props = defineProps({
-  schedule: { type: Object, default: null },
-  servicePackage: { type: Object, default: null },
+  servicePackage: { type: Object, required: true },
 });
 
 defineEmits([...useDialogPluginComponent.emits]);
@@ -167,25 +166,18 @@ const paymentStore = usePaymentStore();
 
 const { platformFees, loadPlatformFees } = usePaymentPlatformFees();
 
-const isServicePackage = computed(() => !!props.servicePackage);
-const item = computed(() => props.servicePackage ?? props.schedule);
+const item = computed(() => props.servicePackage);
 
 const displayProviderName = computed(() => {
-  if (isServicePackage.value) {
-    return item.value.provider?.user?.name ?? item.value.provider_name ?? '';
-  }
-  return item.value.provider_name ?? '';
+  return item.value.provider?.user?.name ?? item.value.provider_name ?? '';
 });
 
 const displayDistrict = computed(() => {
-  if (isServicePackage.value) {
-    return item.value.schedules?.[0]?.address?.district ?? item.value.address?.district ?? '';
-  }
-  return item.value.address?.district ?? '';
+  return item.value.schedules?.[0]?.address?.district ?? item.value.address?.district ?? '';
 });
 
 const displayDate = computed(() => {
-  const src = isServicePackage.value ? item.value.schedules?.[0] : item.value;
+  const src = item.value.schedules?.[0] ?? item.value;
   if (!src) return '';
   if (src.formatted_date) return src.formatted_date;
   const raw = String(src.date || '');
@@ -200,17 +192,11 @@ const displayDate = computed(() => {
 });
 
 const displayStartTime = computed(() => {
-  if (isServicePackage.value) {
-    return item.value.schedules?.[0]?.start_time?.slice(0, 5) ?? '';
-  }
-  return item.value.start_time?.slice(0, 5) ?? '';
+  return item.value.schedules?.[0]?.start_time?.slice(0, 5) ?? '';
 });
 
 const displayEndTime = computed(() => {
-  if (isServicePackage.value) {
-    return item.value.schedules?.[0]?.end_time?.slice(0, 5) ?? '';
-  }
-  return item.value.end_time?.slice(0, 5) ?? '';
+  return item.value.schedules?.[0]?.end_time?.slice(0, 5) ?? '';
 });
 
 const avatarStyle = computed(
@@ -245,10 +231,7 @@ const platformFee = (paymentType) => {
 };
 
 const onGoToPayment = () => {
-  const validPixPayment = isServicePackage.value
-    ? paymentStore.getValidPixPaymentForServicePackage(item.value.id)
-    : paymentStore.getValidPixPayment(item.value.id);
-
+  const validPixPayment = paymentStore.getValidPixPaymentForServicePackage(item.value.id);
   const hasValidPixPayment = !!validPixPayment;
 
   $q.dialog({
@@ -256,9 +239,7 @@ const onGoToPayment = () => {
       ? SchedulePaymentPixDialog
       : SchedulePaymentDialog,
     componentProps: {
-      ...(isServicePackage.value
-        ? { servicePackage: item.value }
-        : { schedule: item.value }),
+      servicePackage: item.value,
       ...(hasValidPixPayment ? { total: pixTotal.value } : {}),
     },
   }).onOk(() => {

+ 6 - 20
src/components/dashboard/SchedulePaymentDialog.vue

@@ -120,13 +120,9 @@ import SchedulePaymentPixDialog from './SchedulePaymentPixDialog.vue'
 import SchedulePaymentProcessingDialog from './SchedulePaymentProcessingDialog.vue'
 
 const props = defineProps({
-  schedule: {
-    type: Object,
-    default: null,
-  },
   servicePackage: {
     type: Object,
-    default: null,
+    required: true,
   },
 })
 
@@ -139,8 +135,7 @@ const store = userStore()
 const paymentStore = usePaymentStore()
 const { platformFees, loadPlatformFees } = usePaymentPlatformFees()
 
-const isServicePackage = computed(() => !!props.servicePackage)
-const item = computed(() => props.servicePackage ?? props.schedule)
+const item = computed(() => props.servicePackage)
 
 const selectedMethod = ref(null)
 const agreedToTerms = ref(false)
@@ -151,10 +146,7 @@ const selectedPaymentType = computed(() => selectedMethod.value === 'pix' ? 'pix
 const selectedTotal = computed(() => getScheduleTotalWithPlatformFee(item.value, selectedPaymentType.value, platformFees.value))
 
 const getAddress = () => {
-  if (isServicePackage.value) {
-    return item.value.schedules?.[0]?.address ?? item.value.address ?? null;
-  }
-  return item.value.address ?? null;
+  return item.value.schedules?.[0]?.address ?? item.value.address ?? null;
 };
 
 const addressTypeLabel = computed(() => {
@@ -209,9 +201,7 @@ const openPixPayment = () => {
   $q.dialog({
     component: SchedulePaymentPixDialog,
     componentProps: {
-      ...(isServicePackage.value
-        ? { servicePackage: item.value }
-        : { schedule: item.value }),
+      servicePackage: item.value,
       total: selectedTotal.value,
     },
   }).onOk(() => {
@@ -229,9 +219,7 @@ const onConfirm = () => {
   $q.dialog({
     component: SchedulePaymentProcessingDialog,
     componentProps: {
-      ...(isServicePackage.value
-        ? { servicePackage: item.value }
-        : { schedule: item.value }),
+      servicePackage: item.value,
       clientPaymentMethodId,
       total: selectedTotal.value,
     },
@@ -243,9 +231,7 @@ const onConfirm = () => {
 onMounted(() => {
   loadPlatformFees().catch(() => {})
 
-  const existingPix = isServicePackage.value
-    ? paymentStore.getValidPixPaymentForServicePackage(item.value.id)
-    : paymentStore.getValidPixPayment(item.value.id)
+  const existingPix = paymentStore.getValidPixPaymentForServicePackage(item.value.id)
 
   if (existingPix) {
     openPixPayment()

+ 10 - 34
src/components/dashboard/SchedulePaymentPixDialog.vue

@@ -110,12 +110,11 @@
 import { computed, ref, onMounted, onUnmounted } from 'vue'
 import { useDialogPluginComponent, useQuasar, copyToClipboard } from 'quasar'
 import { formatCurrency } from 'src/helpers/utils'
-import { getSchedulePixPayment, getServicePackagePix, paySchedule, payServicePackage } from 'src/api/payment'
+import { getServicePackagePix, payServicePackage } from 'src/api/payment'
 import { usePaymentStore } from 'src/stores/payment'
 
 const props = defineProps({
-  schedule: { type: Object, default: null },
-  servicePackage: { type: Object, default: null },
+  servicePackage: { type: Object, required: true },
   total: { type: Number, required: true },
 })
 
@@ -125,8 +124,7 @@ const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginC
 const $q = useQuasar()
 const paymentStore = usePaymentStore()
 
-const isServicePackage = computed(() => !!props.servicePackage)
-const item = computed(() => props.servicePackage ?? props.schedule)
+const item = computed(() => props.servicePackage)
 const itemId = computed(() => item.value.id)
 
 const payment = ref(null)
@@ -173,33 +171,21 @@ const applyPaymentStatus = (nextPayment) => {
   if (nextPayment.status === 'paid') {
     success.value = true
     processing.value = false
-    if (isServicePackage.value) {
-      paymentStore.clearPixPaymentForServicePackage(itemId.value)
-    } else {
-      paymentStore.clearPixPayment(itemId.value)
-    }
+    paymentStore.clearPixPaymentForServicePackage(itemId.value)
     stopPolling()
     return
   }
 
   if (['failed', 'cancelled'].includes(nextPayment.status)) {
     processing.value = false
-    if (isServicePackage.value) {
-      paymentStore.clearPixPaymentForServicePackage(itemId.value)
-    } else {
-      paymentStore.clearPixPayment(itemId.value)
-    }
+    paymentStore.clearPixPaymentForServicePackage(itemId.value)
     stopPolling()
     $q.notify({ type: 'negative', message: nextPayment.failure_message || 'Pagamento Pix não confirmado.' })
     onDialogCancel()
     return
   }
 
-  if (isServicePackage.value) {
-    paymentStore.setPixPaymentForServicePackage(itemId.value, nextPayment)
-  } else {
-    paymentStore.setPixPayment(itemId.value, nextPayment)
-  }
+  paymentStore.setPixPaymentForServicePackage(itemId.value, nextPayment)
 }
 
 const checkPaymentStatus = async () => {
@@ -207,9 +193,7 @@ const checkPaymentStatus = async () => {
 
   pollingInFlight = true
   try {
-    const result = isServicePackage.value
-      ? await getServicePackagePix(itemId.value)
-      : await getSchedulePixPayment(itemId.value)
+    const result = await getServicePackagePix(itemId.value)
     applyPaymentStatus(result)
     updateCountdown()
   } catch (e) {
@@ -237,11 +221,7 @@ const updateCountdown = () => {
   if (!pixExpiresAt.value && totalSeconds.value > 0) totalSeconds.value--
 
   if (pixExpiresAt.value && totalSeconds.value <= 0) {
-    if (isServicePackage.value) {
-      paymentStore.clearPixPaymentForServicePackage(itemId.value)
-    } else {
-      paymentStore.clearPixPayment(itemId.value)
-    }
+    paymentStore.clearPixPaymentForServicePackage(itemId.value)
     stopPolling()
   }
 }
@@ -251,9 +231,7 @@ onMounted(async () => {
   countdownTimer = setInterval(updateCountdown, 1000)
 
   try {
-    const cachedPayment = isServicePackage.value
-      ? paymentStore.getValidPixPaymentForServicePackage(itemId.value)
-      : paymentStore.getValidPixPayment(itemId.value)
+    const cachedPayment = paymentStore.getValidPixPaymentForServicePackage(itemId.value)
 
     if (cachedPayment) {
       applyPaymentStatus(cachedPayment)
@@ -262,9 +240,7 @@ onMounted(async () => {
       return
     }
 
-    const paymentResult = isServicePackage.value
-      ? await payServicePackage(itemId.value, { payment_method: 'pix' })
-      : await paySchedule(itemId.value, { payment_method: 'pix' })
+    const paymentResult = await payServicePackage(itemId.value, { payment_method: 'pix' })
 
     applyPaymentStatus(paymentResult)
     updateCountdown()

+ 8 - 15
src/components/dashboard/SchedulePaymentProcessingDialog.vue

@@ -47,11 +47,10 @@
 import { ref, computed, onMounted } from 'vue'
 import { useDialogPluginComponent, useQuasar } from 'quasar'
 import LogoDiariaSucesso from 'src/assets/diarinho-success-payment.svg';
-import { paySchedule, payServicePackage } from 'src/api/payment'
+import { payServicePackage } from 'src/api/payment'
 
 const props = defineProps({
-  schedule: { type: Object, default: null },
-  servicePackage: { type: Object, default: null },
+  servicePackage: { type: Object, required: true },
   clientPaymentMethodId: { type: Number, required: true },
   total: { type: Number, default: null },
 })
@@ -61,22 +60,16 @@ defineEmits([...useDialogPluginComponent.emits])
 const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginComponent()
 const $q = useQuasar()
 
-const isServicePackage = computed(() => !!props.servicePackage)
-const item = computed(() => props.servicePackage ?? props.schedule)
+const item = computed(() => props.servicePackage)
 
 const success = ref(false)
 
 onMounted(async () => {
   try {
-    const payment = isServicePackage.value
-      ? await payServicePackage(item.value.id, {
-          payment_method: 'credit_card',
-          client_payment_method_id: props.clientPaymentMethodId,
-        })
-      : await paySchedule(item.value.id, {
-          payment_method: 'credit_card',
-          client_payment_method_id: props.clientPaymentMethodId,
-        })
+    const payment = await payServicePackage(item.value.id, {
+      payment_method: 'credit_card',
+      client_payment_method_id: props.clientPaymentMethodId,
+    })
 
     if (payment.status !== 'paid') {
       throw new Error(payment.failure_message || 'Pagamento não confirmado.')
@@ -84,7 +77,7 @@ onMounted(async () => {
 
     success.value = true
   } catch (e) {
-    console.error('Erro ao pagar agendamento:', e)
+    console.error('Erro ao pagar:', e)
     $q.notify({ type: 'negative', message: e?.response?.data?.message ?? e.message ?? 'Erro ao processar pagamento.' })
     onDialogCancel()
   }

+ 1 - 29
src/pages/dashboard/DashboardPage.vue

@@ -19,13 +19,6 @@
           :data="pendingServicePackages"
           :type="'servicePackage'"
           @view-details="openServicePackagePaymentDialog"
-          @cancel="cancelSchedule"
-        />
-        <DashboardPendingSchedules
-          v-if="pendingServicePackages.length === 0 && acceptedSchedules.length > 0"
-          :data="acceptedSchedules"
-          @view-details="openAcceptedDialog"
-          @cancel="cancelSchedule"
         />
         <DashboardTodaySchedules v-if="todaySchedules.length > 0" :data="todaySchedules" @rate="openRatingDialog" />
         <DashboardScrollAreaSchedules />
@@ -58,17 +51,15 @@ import DashboardPendingCustomSchedules from 'src/pages/dashboard/components/Dash
 import DashboardClientProposals from 'src/pages/dashboard/components/DashboardClientProposals.vue';
 import { useRouter } from 'vue-router'
 import { onMounted, ref, computed } from 'vue';
-import { useDialogPluginComponent, useQuasar } from 'quasar';
+import { useQuasar } from 'quasar';
 import { dadosDashboard } from 'src/api/dashboard';
 import { userStore } from 'src/stores/user';
-import ScheduleCancelDialog from 'src/components/dashboard/ScheduleCancelDialog.vue';
 import NextSchedulesDetailsDialog from 'src/components/dashboard/NextSchedulesDetailsDialog.vue';
 import ScheduleRatingDialog from 'src/components/dashboard/ScheduleRatingDialog.vue';
 
 const router = useRouter()
 const $q = useQuasar();
 const store = userStore();
-const { onDialogOK } = useDialogPluginComponent();
 
 const hasPaymentMethods = ref(true);
 const headerBar = ref({});
@@ -86,18 +77,8 @@ const notifications = ref([]);
 const loading = ref(true);
 const showSuccessModal = ref(router.currentRoute.value.fullPath.includes('showSuccessModal'));
 const registrationComplete = computed(() => store.user?.registration_complete ?? true);
-const acceptedSchedules = computed(() => pendingSchedules.value.filter((s) => s.status === 'accepted'));
 
 
-const openAcceptedDialog = (schedule) => {
-  $q.dialog({
-    component: ScheduleAcceptedDialog,
-    componentProps: { schedule }
-  }).onOk(() => {
-    reloadDashboard();
-  });
-};
-
 const openServicePackagePaymentDialog = (servicePackage) => {
   $q.dialog({
     component: ScheduleAcceptedDialog,
@@ -154,15 +135,6 @@ const openNextScheduleDialog = (schedule) => {
   });
 };
 
-const cancelSchedule = (schedule) => {
-  $q.dialog({
-    component: ScheduleCancelDialog,
-    componentProps: { schedule: schedule }
-  }).onDismiss(() => {
-    onDialogOK(reloadDashboard());
-  })
-}
-
 const openRatingDialog = (schedule) => {
   $q.dialog({
     component: ScheduleRatingDialog,

+ 0 - 33
src/stores/payment.js

@@ -10,37 +10,8 @@ const isFutureDate = (date) => {
 };
 
 export const usePaymentStore = defineStore('payment', () => {
-  const pixPaymentsByScheduleId = ref({});
   const pixPaymentsByServicePackageId = ref({});
 
-  const getValidPixPayment = (scheduleId) => {
-    const payment = pixPaymentsByScheduleId.value[scheduleId] ?? null;
-    if (!payment) return null;
-
-    if (!isFutureDate(getPixExpiresAt(payment))) {
-      clearPixPayment(scheduleId);
-      return null;
-    }
-
-    return payment;
-  };
-
-  const setPixPayment = (scheduleId, payment) => {
-    if (!scheduleId || !payment) return;
-    pixPaymentsByScheduleId.value = {
-      ...pixPaymentsByScheduleId.value,
-      [scheduleId]: payment,
-    };
-  };
-
-  const clearPixPayment = (scheduleId) => {
-    if (!scheduleId || !pixPaymentsByScheduleId.value[scheduleId]) return;
-
-    const payments = { ...pixPaymentsByScheduleId.value };
-    delete payments[scheduleId];
-    pixPaymentsByScheduleId.value = payments;
-  };
-
   const getValidPixPaymentForServicePackage = (servicePackageId) => {
     const payment = pixPaymentsByServicePackageId.value[servicePackageId] ?? null;
     if (!payment) return null;
@@ -70,11 +41,7 @@ export const usePaymentStore = defineStore('payment', () => {
   };
 
   return {
-    pixPaymentsByScheduleId,
     pixPaymentsByServicePackageId,
-    getValidPixPayment,
-    setPixPayment,
-    clearPixPayment,
     getValidPixPaymentForServicePackage,
     setPixPaymentForServicePackage,
     clearPixPaymentForServicePackage,