From c3d9d1ae5449548db3d95f027f06c418032a46f4 Mon Sep 17 00:00:00 2001 From: Nathan Stitt Date: Tue, 29 Jun 2021 12:36:05 -0500 Subject: [PATCH] fix refund api request --- shared/src/model/api.ts | 2 +- tutor/specs/models/purchases/purchase.spec.ts | 9 ++++++--- tutor/src/models/purchases/purchase.ts | 16 +++++++++------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/shared/src/model/api.ts b/shared/src/model/api.ts index 5127559da5..11158d294b 100644 --- a/shared/src/model/api.ts +++ b/shared/src/model/api.ts @@ -30,7 +30,7 @@ export class ModelApi { } return null }, - })) //observable.map({}, { deep: false }) + })) @readonly requestCounts = observable({ read: 0, diff --git a/tutor/specs/models/purchases/purchase.spec.ts b/tutor/specs/models/purchases/purchase.spec.ts index 047e59d3f9..20201815c4 100644 --- a/tutor/specs/models/purchases/purchase.spec.ts +++ b/tutor/specs/models/purchases/purchase.spec.ts @@ -1,5 +1,5 @@ import { Purchase, Course } from '../../../src/models' -import { Factory, hydrateModel } from '../../helpers' +import { Factory, hydrateModel, ApiMock } from '../../helpers' describe('Purchase Model', () => { let course!:Course @@ -16,8 +16,11 @@ describe('Purchase Model', () => { expect(purchase.course).toBe(course) }); - test('#onRefunded', () => { - purchase.onRefunded(); + test('#onRefunded', async () => { + ApiMock.mock({ + [`purchases/${purchase.product_instance_uuid}/refund`]: { ok: true }, + }) + await purchase.refund(); expect(course.userStudentRecord!.is_paid).toBe(false); expect(course.userStudentRecord!.prompt_student_to_pay).toBe(true); }); diff --git a/tutor/src/models/purchases/purchase.ts b/tutor/src/models/purchases/purchase.ts index 972794c80a..2ca8298bcc 100644 --- a/tutor/src/models/purchases/purchase.ts +++ b/tutor/src/models/purchases/purchase.ts @@ -1,8 +1,9 @@ import { pick, extend } from 'lodash'; -import { BaseModel, field, model, computed, modelize, NEW_ID } from 'shared/model'; +import { BaseModel, field, model, computed, action, modelize, NEW_ID } from 'shared/model'; import Time from 'shared/model/time'; import S from '../../helpers/string'; import { currentCourses } from '../../../src/models' +import urlFor from '../../api' class Product extends BaseModel { @field uuid = ''; @@ -67,15 +68,16 @@ export class Purchase extends BaseModel { return amount; } - refund() { - return { item_uuid: this.product_instance_uuid, refund_survey: this.refund_survey }; + @action async refund() { + await this.api.request(urlFor('requestRefund', { itemUUID: this.product_instance_uuid }), { + data: { refund_survey: this.refund_survey }, + }) + this.onRefunded() } - onRefunded() { + @action onRefunded() { this.is_refunded = true; - if (this.course) { - this.course.userStudentRecord?.markRefunded(); - } + this.course?.userStudentRecord?.markRefunded(); } }