|
| 1 | +import { Injectable, inject, signal, WritableSignal } from '@angular/core'; |
| 2 | +import { ApiService } from '../../core'; |
| 3 | +import { Subscription } from '@zoneless/shared-types'; |
| 4 | +import { |
| 5 | + CancelSubscriptionInput, |
| 6 | + CreateSubscriptionInput, |
| 7 | + ResumeSubscriptionInput, |
| 8 | + UpdateSubscriptionInput, |
| 9 | +} from '@zoneless/shared-schemas'; |
| 10 | + |
| 11 | +@Injectable({ |
| 12 | + providedIn: 'root', |
| 13 | +}) |
| 14 | +export class SubscriptionService { |
| 15 | + private readonly api = inject(ApiService); |
| 16 | + |
| 17 | + loading: WritableSignal<boolean> = signal(false); |
| 18 | + |
| 19 | + async CreateSubscription( |
| 20 | + data: CreateSubscriptionInput |
| 21 | + ): Promise<Subscription> { |
| 22 | + this.loading.set(true); |
| 23 | + try { |
| 24 | + return await this.api.Call<Subscription>('POST', `subscriptions`, data); |
| 25 | + } finally { |
| 26 | + this.loading.set(false); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + async GetSubscription( |
| 31 | + subscriptionId: string, |
| 32 | + expand: string[] = ['customer'] |
| 33 | + ): Promise<Subscription> { |
| 34 | + this.loading.set(true); |
| 35 | + try { |
| 36 | + const expandQuery = |
| 37 | + expand.length > 0 ? `?expand=${expand.join(',')}` : ''; |
| 38 | + return await this.api.Call<Subscription>( |
| 39 | + 'GET', |
| 40 | + `subscriptions/${subscriptionId}${expandQuery}` |
| 41 | + ); |
| 42 | + } finally { |
| 43 | + this.loading.set(false); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + async UpdateSubscription( |
| 48 | + subscriptionId: string, |
| 49 | + data: UpdateSubscriptionInput |
| 50 | + ): Promise<Subscription> { |
| 51 | + this.loading.set(true); |
| 52 | + try { |
| 53 | + return await this.api.Call<Subscription>( |
| 54 | + 'POST', |
| 55 | + `subscriptions/${subscriptionId}`, |
| 56 | + data |
| 57 | + ); |
| 58 | + } finally { |
| 59 | + this.loading.set(false); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + async CancelSubscription( |
| 64 | + subscriptionId: string, |
| 65 | + data: CancelSubscriptionInput = {} |
| 66 | + ): Promise<Subscription> { |
| 67 | + this.loading.set(true); |
| 68 | + try { |
| 69 | + return await this.api.Call<Subscription>( |
| 70 | + 'DELETE', |
| 71 | + `subscriptions/${subscriptionId}`, |
| 72 | + data |
| 73 | + ); |
| 74 | + } finally { |
| 75 | + this.loading.set(false); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + async ResumeSubscription( |
| 80 | + subscriptionId: string, |
| 81 | + data: ResumeSubscriptionInput = {} |
| 82 | + ): Promise<Subscription> { |
| 83 | + this.loading.set(true); |
| 84 | + try { |
| 85 | + return await this.api.Call<Subscription>( |
| 86 | + 'POST', |
| 87 | + `subscriptions/${subscriptionId}/resume`, |
| 88 | + data |
| 89 | + ); |
| 90 | + } finally { |
| 91 | + this.loading.set(false); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments