- Integrated `SubscriptionInfoBar` component into the profile page for displaying subscription details. - Updated GraphQL types to include subscription-related fields and filters. - Enhanced the profile data management by adding subscription handling capabilities. - Added a new utility function `getRemainingDays` to calculate remaining days until a specified date.
165 lines
4.3 KiB
TypeScript
165 lines
4.3 KiB
TypeScript
import { getClientWithToken } from '../apollo/client';
|
|
import * as GQL from '../types';
|
|
import { BaseService } from './base';
|
|
import { OrdersService } from './orders';
|
|
import { type VariablesOf } from '@graphql-typed-document-node/core';
|
|
import dayjs from 'dayjs';
|
|
|
|
export class SubscriptionsService extends BaseService {
|
|
async createSubscription(variables: VariablesOf<typeof GQL.CreateSubscriptionDocument>) {
|
|
await this.checkIsBanned();
|
|
|
|
const { mutate } = await getClientWithToken();
|
|
|
|
const mutationResult = await mutate({
|
|
mutation: GQL.CreateSubscriptionDocument,
|
|
variables,
|
|
});
|
|
|
|
const error = mutationResult.errors?.at(0);
|
|
if (error) throw new Error(error.message);
|
|
|
|
return mutationResult.data;
|
|
}
|
|
|
|
async createSubscriptionHistory(
|
|
variables: VariablesOf<typeof GQL.CreateSubscriptionHistoryDocument>,
|
|
) {
|
|
await this.checkIsBanned();
|
|
|
|
const { mutate } = await getClientWithToken();
|
|
|
|
const mutationResult = await mutate({
|
|
mutation: GQL.CreateSubscriptionHistoryDocument,
|
|
variables,
|
|
});
|
|
|
|
const error = mutationResult.errors?.at(0);
|
|
if (error) throw new Error(error.message);
|
|
|
|
return mutationResult.data;
|
|
}
|
|
|
|
async getSubscription(variables: VariablesOf<typeof GQL.GetSubscriptionDocument>) {
|
|
await this.checkIsBanned();
|
|
|
|
const { query } = await getClientWithToken();
|
|
|
|
const result = await query({
|
|
query: GQL.GetSubscriptionDocument,
|
|
variables,
|
|
});
|
|
|
|
const subscription = result.data.subscriptions.at(0);
|
|
|
|
const { maxOrdersPerMonth, remainingOrdersCount } = await this.getRemainingOrdersCount();
|
|
|
|
return { maxOrdersPerMonth, remainingOrdersCount, subscription };
|
|
}
|
|
|
|
async getSubscriptionHistory(variables: VariablesOf<typeof GQL.GetSubscriptionHistoryDocument>) {
|
|
await this.checkIsBanned();
|
|
|
|
const { query } = await getClientWithToken();
|
|
|
|
const result = await query({
|
|
query: GQL.GetSubscriptionHistoryDocument,
|
|
variables,
|
|
});
|
|
|
|
const subscriptionHistories = result.data.subscriptionHistories;
|
|
|
|
return { subscriptionHistories };
|
|
}
|
|
|
|
async getSubscriptionPrices(variables?: VariablesOf<typeof GQL.GetSubscriptionPricesDocument>) {
|
|
await this.checkIsBanned();
|
|
|
|
const { query } = await getClientWithToken();
|
|
|
|
const result = await query({
|
|
query: GQL.GetSubscriptionPricesDocument,
|
|
variables,
|
|
});
|
|
|
|
return result.data;
|
|
}
|
|
|
|
async getSubscriptionSettings() {
|
|
await this.checkIsBanned();
|
|
|
|
const { query } = await getClientWithToken();
|
|
|
|
const result = await query({
|
|
query: GQL.GetSubscriptionSettingsDocument,
|
|
});
|
|
|
|
return result.data;
|
|
}
|
|
|
|
async updateSubscription(variables: VariablesOf<typeof GQL.UpdateSubscriptionDocument>) {
|
|
await this.checkIsBanned();
|
|
|
|
const { mutate } = await getClientWithToken();
|
|
|
|
const mutationResult = await mutate({
|
|
mutation: GQL.UpdateSubscriptionDocument,
|
|
variables,
|
|
});
|
|
|
|
const error = mutationResult.errors?.at(0);
|
|
if (error) throw new Error(error.message);
|
|
|
|
return mutationResult.data;
|
|
}
|
|
|
|
async updateSubscriptionHistory(
|
|
variables: VariablesOf<typeof GQL.UpdateSubscriptionHistoryDocument>,
|
|
) {
|
|
await this.checkIsBanned();
|
|
|
|
const { mutate } = await getClientWithToken();
|
|
|
|
const mutationResult = await mutate({
|
|
mutation: GQL.UpdateSubscriptionHistoryDocument,
|
|
variables,
|
|
});
|
|
|
|
const error = mutationResult.errors?.at(0);
|
|
if (error) throw new Error(error.message);
|
|
|
|
return mutationResult.data;
|
|
}
|
|
|
|
private async getRemainingOrdersCount() {
|
|
const ordersService = new OrdersService(this._user);
|
|
|
|
const now = dayjs();
|
|
|
|
const { orders } = await ordersService.getOrders({
|
|
filters: {
|
|
datetime_end: {
|
|
lte: now.endOf('month').toISOString(),
|
|
},
|
|
datetime_start: {
|
|
gte: now.startOf('month').toISOString(),
|
|
},
|
|
|
|
state: {
|
|
eq: GQL.Enum_Order_State.Completed,
|
|
},
|
|
},
|
|
});
|
|
|
|
const { subscriptionSetting } = await this.getSubscriptionSettings();
|
|
|
|
if (!subscriptionSetting) throw new Error('Subscription setting not found');
|
|
|
|
const { maxOrdersPerMonth } = subscriptionSetting;
|
|
|
|
const remainingOrdersCount = maxOrdersPerMonth - (orders?.length ?? 0);
|
|
|
|
return { maxOrdersPerMonth, remainingOrdersCount };
|
|
}
|
|
}
|