1// Copyright (C) 2020 Storj Labs, Inc.
2// See LICENSE for copying information.
3
4import {
5    EstimatedPayout,
6    PaymentInfoParameters,
7    PayoutApi,
8    PayoutPeriod,
9    Paystub,
10    SatelliteHeldHistory,
11    SatellitePayoutForPeriod,
12    TotalPayments,
13    TotalPaystubForPeriod,
14} from '@/storagenode/payouts/payouts';
15
16/**
17 * PayoutService is used to store and handle node paystub information.
18 * PayoutService exposes a business logic related to payouts.
19 */
20export class PayoutService {
21    private readonly payouts: PayoutApi;
22
23    public constructor(api: PayoutApi) {
24        this.payouts = api;
25    }
26
27    /**
28     * Gets summary of paystubs for given period.
29     * @param start period start
30     * @param end period end
31     * @param satelliteId
32     */
33    public async paystubSummaryForPeriod(start: PayoutPeriod | null, end: PayoutPeriod, satelliteId?: string): Promise<TotalPaystubForPeriod> {
34        const paystubs: Paystub[] = await this.payouts.getPaystubsForPeriod(new PaymentInfoParameters(start, end, satelliteId));
35
36        return new TotalPaystubForPeriod(paystubs);
37    }
38
39    /**
40     * Gets held and paid summary for given period.
41     * @param start period start
42     * @param end period end
43     * @param satelliteId
44     */
45    public async totalPayments(start: PayoutPeriod, end: PayoutPeriod, satelliteId: string): Promise<TotalPayments> {
46        const paystubs: Paystub[] = await this.payouts.getPaystubsForPeriod(new PaymentInfoParameters(start, end, satelliteId));
47
48        return new TotalPayments(paystubs);
49    }
50
51    /**
52     * Gets list of payout periods that have paystubs for selected satellite.
53     * If satelliteId is not provided returns periods for all satellites.
54     * @param satelliteId
55     */
56    public async availablePeriods(satelliteId: string): Promise<PayoutPeriod[]> {
57        return await this.payouts.getPayoutPeriods(satelliteId);
58    }
59
60    /**
61     * Gets list of payout history items for given period by satellites.
62     * @param payoutHistoryPeriod year and month representation
63     */
64    public async payoutHistory(payoutHistoryPeriod: string): Promise<SatellitePayoutForPeriod[]> {
65        return await this.payouts.getPayoutHistory(payoutHistoryPeriod);
66    }
67
68    /**
69     * Gets list of held history for all satellites.
70     */
71    public async allSatellitesHeldHistory(): Promise<SatelliteHeldHistory[]> {
72        return await this.payouts.getHeldHistory();
73    }
74
75    /**
76     * Gets estimated payout when no data in paystub.
77     * @param satelliteId
78     */
79    public async estimatedPayout(satelliteId: string): Promise<EstimatedPayout> {
80        return await this.payouts.getEstimatedPayout(satelliteId);
81    }
82}
83