Skip to content

Commit 633c9fa

Browse files
glamboyosamnida
andauthored
Resolve "Use new APIs for add, update, cancel methods" (#693)
Co-authored-by: mnida <[email protected]>
1 parent bb9a175 commit 633c9fa

5 files changed

Lines changed: 70 additions & 86 deletions

File tree

frontend/src/api/api.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ export const Customer = {
182182
): Promise<SubscriptionType> =>
183183
requests.post(`app/subscriptions/${subscription_id}/cancel/`, post),
184184
switchPlanSubscription: (
185-
subscription_id: string,
186-
post: SwitchPlanSubscriptionBody
185+
post: components["schemas"]["SubscriptionRecordSwitchPlanRequest"],
186+
subscription_id: string
187187
): Promise<SubscriptionType> =>
188188
requests.post(`app/subscriptions/${subscription_id}/switch_plan/`, post),
189189
changeSubscriptionPlan: (
@@ -196,23 +196,23 @@ export const Customer = {
196196
): Promise<SubscriptionType> =>
197197
requests.post(`app/subscriptions/update/`, post, params),
198198
turnSubscriptionAutoRenewOff: (
199-
post: TurnSubscriptionAutoRenewOffType,
200-
params?: {
201-
customer_id?: string;
202-
plan_id?: string;
203-
subscription_filters?: { property_name: string; value: string }[];
204-
}
199+
post: components["schemas"]["SubscriptionRecordUpdateRequest"],
200+
subscription_id: string
205201
): Promise<SubscriptionType> =>
206-
requests.post(`app/subscriptions/update/`, post, params),
202+
requests.post(`app/subscriptions/${subscription_id}/update/`, post),
207203
createSubscriptionAddOns: (
208-
body: CreateSubscriptionAddOnBody
204+
body: components["schemas"]["AddOnSubscriptionRecordCreateRequest"],
205+
subscription_id: string
209206
): Promise<CreateSubscriptionAddOnType> =>
210-
requests.post(`app/subscriptions/addons/add/`, body),
207+
requests.post(`app/subscriptions/${subscription_id}/addons/attach/`, body),
211208
cancelCreateSubscriptionAddOns: (
212209
body: CancelCreateSubscriptionAddOnBody,
213-
params: CancelCreateSubscriptionAddOnQueryParams
210+
path_params: { subscription_id: string; addon_id: string }
214211
): Promise<CreateSubscriptionAddOnType[]> =>
215-
requests.post(`app/subscriptions/addons/cancel/`, body, params),
212+
requests.post(
213+
`app/subscriptions/${path_params.subscription_id}/addons/${path_params.addon_id}/cancel/`,
214+
body
215+
),
216216
};
217217

218218
export const AddOn = {

frontend/src/components/Addons/AddonsDetails/AddOnComponents.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { QueryClient, useMutation } from "react-query";
33
import { toast } from "react-toastify";
44
import { Table, Typography, Modal, Button, InputNumber } from "antd";
55
import { Plan } from "../../../api/api";
6-
import { AddOnType } from "../../../types/addon-type";
76
import { AlertType, CreateAlertType } from "../../../types/alert-type";
87
import { Component, Tier } from "../../../types/plan-type";
98
import Select from "../../base/Select/Select";

frontend/src/components/Customers/CustomerDetail.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { CurrencyType } from "../../types/pricing-unit-type";
3232
import { PageLayout } from "../base/PageLayout";
3333
import { QueryErrors } from "../../types/error-response-types";
3434
import { DeleteOutlined } from "@ant-design/icons";
35+
import { components } from "../../gen-types";
3536

3637
type CustomerDetailsParams = {
3738
customerId: string;
@@ -134,8 +135,10 @@ function CustomerDetail() {
134135
);
135136

136137
const changeSubscriptionPlanMutation = useMutation(
137-
(obj: { params: object; post: ChangeSubscriptionPlanType }) =>
138-
Customer.changeSubscriptionPlan(obj.post, obj.params),
138+
(obj: {
139+
post: components["schemas"]["SubscriptionRecordSwitchPlanRequest"];
140+
subscription_id: string;
141+
}) => Customer.switchPlanSubscription(obj.post, obj.subscription_id),
139142
{
140143
onSuccess: () => {
141144
queryClient.invalidateQueries(["customer_list"]);
@@ -152,8 +155,10 @@ function CustomerDetail() {
152155
);
153156

154157
const turnSubscriptionAutoRenewOffMutation = useMutation(
155-
(obj: { params: object; post: TurnSubscriptionAutoRenewOffType }) =>
156-
Customer.turnSubscriptionAutoRenewOff(obj.post, obj.params),
158+
(obj: {
159+
subscription_id: string;
160+
post: components["schemas"]["SubscriptionRecordUpdateRequest"];
161+
}) => Customer.turnSubscriptionAutoRenewOff(obj.post, obj.subscription_id),
157162
{
158163
onSuccess: () => {
159164
queryClient.invalidateQueries(["customer_list"]);
@@ -176,22 +181,19 @@ function CustomerDetail() {
176181
});
177182
};
178183

179-
const changeSubscriptionPlan = (
180-
params: object,
181-
props: ChangeSubscriptionPlanType
182-
) => {
184+
const changeSubscriptionPlan = (params: object, subscription_id) => {
183185
changeSubscriptionPlanMutation.mutate({
184-
params,
185-
post: props,
186+
post: params,
187+
subscription_id
186188
});
187189
};
188190

189191
const turnSubscriptionAutoRenewOff = (
190-
params: object,
191-
props: TurnSubscriptionAutoRenewOffType
192+
subscription_id: string,
193+
props: components["schemas"]["SubscriptionRecordUpdateRequest"]
192194
) => {
193195
turnSubscriptionAutoRenewOffMutation.mutate({
194-
params,
196+
subscription_id,
195197
post: props,
196198
});
197199
};

frontend/src/components/Customers/CustomerSubscriptionView.tsx

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable react/no-unstable-nested-components */
12
/* eslint-disable no-case-declarations */
23
/* eslint-disable no-shadow */
34
/* eslint-disable @typescript-eslint/no-non-null-assertion */
@@ -55,20 +56,21 @@ import ChevronDown from "../base/ChevronDown";
5556
import CancelMenu from "./CancelMenu";
5657
import SwitchMenu from "./SwitchMenu";
5758
import CustomPagination from "../CustomPagination/CustomPagination";
59+
import { components } from "../../gen-types";
5860

5961
interface Props {
6062
customer_id: string;
6163
subscriptions: SubscriptionType[];
6264
plans: PlanType[] | undefined;
6365
onAutoRenewOff: (
64-
params: object,
65-
props: TurnSubscriptionAutoRenewOffType
66+
subscription_id: string,
67+
props: components["schemas"]["SubscriptionRecordUpdateRequest"]
6668
) => void;
67-
onCancel: (
68-
props: CancelSubscriptionBody,
69-
params: CancelSubscriptionQueryParams
69+
onCancel: (props: CancelSubscriptionBody, subscription_id: string) => void;
70+
onPlanChange: (
71+
params: components["schemas"]["SubscriptionRecordSwitchPlanRequest"],
72+
subscription_id: string
7073
) => void;
71-
onPlanChange: (params: object, props: ChangeSubscriptionPlanType) => void;
7274
onCreate: (props: CreateSubscriptionType) => void;
7375
}
7476

@@ -157,9 +159,12 @@ const SubscriptionView: FC<Props> = ({
157159
>(["add-ons"], () => AddOn.getAddOns().then((res) => res), {
158160
refetchOnMount: "always",
159161
});
162+
160163
const mutation = useMutation(
161-
(add_on: CreateSubscriptionAddOnBody) =>
162-
Customer.createSubscriptionAddOns(add_on),
164+
(obj: {
165+
add_on: components["schemas"]["AddOnSubscriptionRecordCreateRequest"];
166+
subscription_id: string;
167+
}) => Customer.createSubscriptionAddOns(obj.add_on, obj.subscription_id),
163168
{
164169
onSuccess: () => {
165170
toast.success("Successfully Created Subscription Add-on", {
@@ -211,17 +216,10 @@ const SubscriptionView: FC<Props> = ({
211216
setShowModal(false);
212217
};
213218

214-
const turnAutoRenewOff = (plan_id, subscription_filters) => {
215-
onAutoRenewOff(
216-
{
217-
plan_id,
218-
subscription_filters,
219-
customer_id,
220-
},
221-
{
222-
turn_off_auto_renew: true,
223-
}
224-
);
219+
const turnAutoRenewOff = (subscription_id: string) => {
220+
onAutoRenewOff(subscription_id, {
221+
turn_off_auto_renew: true,
222+
});
225223
setShowModal(false);
226224
};
227225

@@ -269,21 +267,8 @@ const SubscriptionView: FC<Props> = ({
269267
return acc;
270268
}, [] as PlanOption[]);
271269

272-
const onChange = (
273-
value: string,
274-
plan_id: string,
275-
subscription_filters: object[]
276-
) => {
277-
onPlanChange(
278-
{
279-
plan_id,
280-
customer_id,
281-
subscription_filters,
282-
},
283-
{
284-
replace_plan_id: value as string,
285-
}
286-
);
270+
const onChange = (value: string, subscription_id: string) => {
271+
onPlanChange({ switch_plan_id: value as string }, subscription_id);
287272
};
288273

289274
const handleAttachPlanSubmit = () => {
@@ -302,13 +287,10 @@ const SubscriptionView: FC<Props> = ({
302287
}
303288
form.resetFields();
304289
};
305-
const submitAddOns = () => {
290+
const submitAddOns = (subscription_id: string) => {
306291
const body = {
307-
attach_to_customer_id: customer_id,
308-
attach_to_plan_id: attachToPlanId,
309-
attach_to_subscription_filters: attachToSubscriptionFilters || [],
310-
addon_id: addOnId,
311-
quantity,
292+
add_on: { addon_id: addOnId, quantity },
293+
subscription_id,
312294
};
313295

314296
mutation.mutate(body);
@@ -479,20 +461,20 @@ const SubscriptionView: FC<Props> = ({
479461
<CustomerCard.Block>
480462
<CustomerCard.Item>
481463
<div className="font-normal text-card-text font-alliance whitespace-nowrap leading-4">
482-
Plan ID
464+
Subscription ID
483465
</div>
484466
<div className="flex gap-1 !text-card-grey font-menlo">
485467
{" "}
486468
<div>
487469
{createShortenedText(
488-
subPlan.billing_plan.plan_id as string,
470+
subPlan.subscription_id as string,
489471
windowWidth >= 2500
490472
)}
491473
</div>
492474
<CopyText
493475
showIcon
494476
onlyIcon
495-
textToCopy={subPlan.billing_plan.plan_id as string}
477+
textToCopy={subPlan.subscription_id as string}
496478
/>
497479
</div>
498480
</CustomerCard.Item>
@@ -641,11 +623,10 @@ const SubscriptionView: FC<Props> = ({
641623
onClick={() => {
642624
onChange(
643625
cascaderOptions?.value as string,
644-
cascaderOptions?.plan_id as string,
645-
cascaderOptions!.subscriptionFilters
626+
subPlan.subscription_id
646627
);
647628
setShowModal(false);
648-
setCascaderOptions(null);
629+
setCascaderOptions(undefined);
649630
}}
650631
>
651632
Switch
@@ -666,7 +647,9 @@ const SubscriptionView: FC<Props> = ({
666647
borderColor: "#C3986B",
667648
}}
668649
disabled={addOnId.length < 1}
669-
onClick={submitAddOns}
650+
onClick={() => {
651+
submitAddOns(selectedSubPlan.subscription_id);
652+
}}
670653
>
671654
Add
672655
</Button>,
@@ -681,10 +664,7 @@ const SubscriptionView: FC<Props> = ({
681664
type="primary"
682665
className="!bg-rose-600 border !border-rose-600"
683666
onClick={() => {
684-
turnAutoRenewOff(
685-
selectedSubPlan.billing_plan.plan_id,
686-
selectedSubPlan.subscription_filters
687-
);
667+
turnAutoRenewOff(selectedSubPlan!.subscription_id);
688668
}}
689669
>
690670
Cancel Renewal
@@ -700,7 +680,7 @@ const SubscriptionView: FC<Props> = ({
700680
type="primary"
701681
className="!bg-rose-600 border !border-rose-600"
702682
onClick={() => {
703-
cancelSubscription(selectedSubPlan?.subscription_id);
683+
cancelSubscription(selectedSubPlan!.subscription_id);
704684
}}
705685
>
706686
Cancel Plan

frontend/yarn.lock

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ amdefine@>=0.0.4:
14131413
resolved "https://registry.npmjs.org/amdefine/-/amdefine-1.0.1.tgz"
14141414
integrity sha512-S2Hw0TtNkMJhIabBwIojKL9YHO5T0n5eNqWJ7Lrlel/zDbftQpxpapi8tZs3X1HWa+u+QeydGmzzNU0m09+Rcg==
14151415

1416-
ansi-colors@^4.1.3:
1416+
ansi-colors@^4.1.1, ansi-colors@^4.1.3:
14171417
version "4.1.3"
14181418
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.3.tgz#37611340eb2243e70cc604cad35d63270d48781b"
14191419
integrity sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==
@@ -1891,6 +1891,7 @@ busboy@^1.6.0:
18911891
integrity sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==
18921892
dependencies:
18931893
streamsearch "^1.1.0"
1894+
18941895
cachedir@^2.3.0:
18951896
version "2.3.0"
18961897
resolved "https://registry.yarnpkg.com/cachedir/-/cachedir-2.3.0.tgz#0c75892a052198f0b21c7c1804d8331edfcae0e8"
@@ -2120,12 +2121,7 @@ clone@^1.0.2:
21202121
resolved "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz"
21212122
integrity sha512-JQHZ2QMW6l3aH/j6xCqQThY/9OH4D/9ls34cgkUBiEeocRTU04tHfKPBsUK1PqZCUQM7GiA0IIXJSuXHI64Kbg==
21222123

2123-
clsx@^1.1.1:
2124-
version "1.2.1"
2125-
resolved "https://registry.npmjs.org/clsx/-/clsx-1.2.1.tgz"
2126-
integrity sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg==
2127-
2128-
clsx@^1.2.1:
2124+
clsx@^1.1.1, clsx@^1.2.1:
21292125
version "1.2.1"
21302126
resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.2.1.tgz#0ddc4a20a549b59c93a4116bb26f5294ca17dc12"
21312127
integrity sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg==
@@ -6873,6 +6869,13 @@ supports-color@^7.1.0:
68736869
dependencies:
68746870
has-flag "^4.0.0"
68756871

6872+
supports-color@^8.1.1:
6873+
version "8.1.1"
6874+
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-8.1.1.tgz#cd6fc17e28500cff56c1b86c0a7fd4a54a73005c"
6875+
integrity sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==
6876+
dependencies:
6877+
has-flag "^4.0.0"
6878+
68766879
supports-color@^9.3.1:
68776880
version "9.3.1"
68786881
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-9.3.1.tgz#34e4ad3c71c9a39dae3254ecc46c9b74e89e15a6"

0 commit comments

Comments
 (0)