All files / libs/payout-definition/challenges/state/src/lib/api-client challenges.api-client.ts

73.75% Statements 59/80
100% Branches 3/3
60% Functions 3/5
73.75% Lines 59/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 811x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x                         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { http } from '@amalia/core/http/client';
import { type Payment } from '@amalia/core/types';
import { type Period } from '@amalia/payout-definition/periods/types';
import {
  type Challenge,
  type ChallengeLaunchNotificationStatus,
  type Plan,
  type Rule,
  type RuleConfiguration,
} from '@amalia/payout-definition/plans/types';
import { type UserComputed, type UserContract } from '@amalia/tenants/users/types';
 
const e = encodeURIComponent;
 
export abstract class ChallengeApiClient {
  private static readonly API_ENDPOINT = '/challenges';
 
  public static async getChallengeByRulePeriod({
    ruleId,
    periodId,
  }: {
    ruleId: Rule['id'];
    periodId: Period['id'];
  }): Promise<Challenge | null> {
    const { data } = await http.get<Challenge | null>('/challenges', { params: { ruleId, periodId } });
    return data;
  }
 
  public static async getChallengePayments({
    challengeId,
    periodId,
  }: {
    challengeId: Challenge['id'];
    periodId: Period['id'];
  }) {
    return (
      await http.get<Payment[]>(`${ChallengeApiClient.API_ENDPOINT}/${e(challengeId)}/payments`, {
        params: { periodId },
      })
    ).data;
  }
 
  public static async getUsersToNotify({
    planId,
    challengeStartDate,
  }: {
    planId: Plan['id'];
    challengeStartDate: RuleConfiguration['challengeStartDate'];
  }): Promise<UserComputed[]> {
    const { data } = await http.get<UserComputed[]>(`${ChallengeApiClient.API_ENDPOINT}/users-to-notify`, {
      params: { planId, challengeStartDate },
    });
    return data;
  }
 
  public static async sendChallengeLaunchNotification({
    challengeRuleId,
    userIds,
  }: {
    challengeRuleId: Rule['id'];
    userIds: UserContract['id'][];
  }): Promise<void> {
    await http.post(`${ChallengeApiClient.API_ENDPOINT}/launch-challenge`, {
      userIds,
      challengeRuleId,
    });
  }
 
  public static async getLaunchChallengeNotificationsStatus({
    ruleId,
  }: {
    ruleId: Rule['id'];
  }): Promise<ChallengeLaunchNotificationStatus> {
    const { data } = await http.get<ChallengeLaunchNotificationStatus>(
      `${ChallengeApiClient.API_ENDPOINT}/launch-challenge-notification-status`,
      { params: { ruleId } },
    );
    return data;
  }
}