import AuthRequestProxy = require("../portalAuthorization/AuthRequestProxy"); import { IAuthProvider } from "../typings/IAuthProvider"; import { IClusterClientProvider } from "../typings/IClusterClientProvider"; import { Contact, ContactSettings, DeliverySchedule, ContactAddress } from "./Contact"; import { IClusterClientResponse } from "../typings/IClusterClient"; export type TargetType = "userId" | "organizationId" | "organizationGroupId"; export type ContactBindInfo = { "user-id": string; status?: string; address: ContactAddress; "channel-group-id": string; "origin-contact-id"?: string; timezone?: string; schedule?: DeliverySchedule; }; export type SubscriptionsQuery = { userId: string | null; contactId: string | null; targetType: TargetType | null; targetId: string | null; channelGroupId: string | null; channels: string | null; tags: string | null; }; export type SubscriptionInfo = { "contact-id": string; "target-type": TargetType | null; "target-id": string | null; "channel-id": string; enabled: boolean | null; }; export type SubscriptionTarget = { "target-type": TargetType; "target-id": string | null; }; export interface SubscriptionsClientDeps { authProvider: IAuthProvider; clusterClientProvider: IClusterClientProvider; } export class SubscriptionsClient { private _http: AuthRequestProxy; constructor({ clusterClientProvider, authProvider }: SubscriptionsClientDeps) { const clusterClient = clusterClientProvider.createClusterClient("api.subs"); this._http = new AuthRequestProxy({ clusterClient, authProvider }); } async getContacts( userId: string | null, addressType: string | null, channelGroupId: string | null ) { return this._http.send<{ items: Contact[]; links: string[] }>({ url: `/v1/contacts`, method: "GET", params: { user_id: userId, address_type: addressType, channel_group_id: channelGroupId } }); } async bindContact(bindInfo: ContactBindInfo) { return this._http.send({ url: `/v1/contacts`, method: "POST", data: bindInfo }); } async getContact(userId: string, contactId: string) { userId = userId.replace(/-/g, ""); contactId = contactId.replace(/-/g, ""); return this._http.send({ url: `/v1/contacts/${userId}-${contactId}`, method: "GET" }); } async updateContactSettings( userId: string, contactId: string, channelGroupId: string, contactSettings: ContactSettings ) { userId = userId.replace(/-/g, ""); contactId = contactId.replace(/-/g, ""); return this._http.send({ url: `/v1/contacts/${userId}-${contactId}/settings/${channelGroupId}`, method: "PUT", data: contactSettings }); } async removeContactSettings( userId: string, contactId: string, channelGroupId: string ) { userId = userId.replace(/-/g, ""); contactId = contactId.replace(/-/g, ""); return this._http.send({ url: `/v1/contacts/${userId}-${contactId}/settings/${channelGroupId}`, method: "DELETE" }); } async requestConfirmation( userId: string, contactId: string, channelGroupId?: string ) { userId = userId.replace(/-/g, ""); contactId = contactId.replace(/-/g, ""); return this._http.send({ url: `/v1/contacts/${userId}-${contactId}/confirm`, method: "POST", params: { channel_group_id: channelGroupId } }); } async confirmEmail( userId: string, contactId: string, confirmCode: string, channelGroupId?: string ) { userId = userId.replace(/-/g, ""); contactId = contactId.replace(/-/g, ""); return this._http.send({ url: `/v1/contacts/${userId}-${contactId}/confirm-email/${confirmCode}`, method: "GET", params: { channel_group_id: channelGroupId } }); } async getContactsByTargetSubscriptions( targetType: string, targetId: string, addressType?: string, channelGroupId?: string ) { return this._http.send<{ items: Contact[]; links: string[] }>({ url: "/v1/contacts/by-target-subscriptions", method: "GET", params: { target_type: targetType, target_id: targetId, address_type: addressType, channel_group_id: channelGroupId } }); } async getTelegramUserInfo(telegramUserId: string) { return this._http.send({ url: `/v1/telegram/users/${telegramUserId}`, method: "GET" }); } async getViberUserInfo(viberUserId: string) { return this._http.send({ url: `/v1/viber/users/${viberUserId}`, method: "GET" }); } async getSubscriptions(query: SubscriptionsQuery) { return this._http.send<{ items: SubscriptionInfo[] }>({ url: "/v1/subscriptions", method: "GET", params: { user_id: query.userId, contact_id: query.contactId, target_type: query.targetType, target_id: query.targetId, channel_group_id: query.channelGroupId, channels: query.channels, tags: query.tags } }); } async changeSubscriptions(subscriptionChangeInfos: Array) { const data = { items: subscriptionChangeInfos }; return this._http.send({ url: "/v1/subscriptions", method: "POST", data }); } async checkSubscription( userId: string, contactId: string, channelId: string, subscriptionTargets?: Array ) { const data = { "user-id": userId, "contact-id": contactId, "channel-id": channelId, targets: subscriptionTargets }; return this._http.send({ url: `/v1/test`, method: "POST", data }); } }