Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
feat: get subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Jun 17, 2020
1 parent 220168b commit 54d8d20
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
63 changes: 60 additions & 3 deletions src/sections/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Codec, enumeration, exactly, GetInterface, string } from 'purify-ts'
import { boolean, Codec, enumeration, exactly, GetInterface, string } from 'purify-ts'

import { ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
Expand All @@ -15,11 +15,20 @@ export type AttributeKeyValueKeys = 'sqsQueueUrl'
export type NotificationType =
| 'AnyOfferChanged'
| 'FeedProcessingFinished'
| 'FeedProcessingFinished'
| 'FeePromotion'
| 'FulfillmentOrderStatus'
| 'ReportProcessingFinished'

enum NotificationTypeEnum {
AnyOfferChanged = 'AnyOfferChanged',
FeedProcessingFinished = 'FeedProcessingFinished',
FeePromotion = 'FeePromotion',
FulfillmentOrderStatus = 'FulfillmentOrderStatus',
ReportProcessingFinished = 'ReportProcessingFinished',
}

const NotificationType = enumeration(NotificationTypeEnum)

interface MarketplaceIdAndDestinationOnlyParameters {
MarketplaceId: string
Destination: Destination
Expand Down Expand Up @@ -112,14 +121,62 @@ const CreateSubscriptionResponse = Codec.interface({
}),
})

interface GetSubscriptionParameters {
MarketplaceId: string
NotificationType: NotificationType
Destination: Destination
}

const Subscription = Codec.interface({
NotificationType,
Destination,
IsEnabled: boolean,
})

const GetSubscription = Codec.interface({
Subscription,
})

type GetSubscription = GetInterface<typeof GetSubscription>

const GetSubscriptionResponse = Codec.interface({
GetSubscriptionResponse: Codec.interface({
GetSubscriptionResult: GetSubscription,
}),
})
export class Subscriptions {
constructor(private httpClient: HttpClient) {}

async getSubscription(
parameters: GetSubscriptionParameters,
): Promise<[GetSubscription, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Subscriptions,
version: SUBSCRIPTIONS_API_VERSION,
action: 'GetSubscription',
parameters: {
MarketplaceId: parameters.MarketplaceId,
NotificationType: parameters.NotificationType,
Destination: {
DeliveryChannel: parameters.Destination.DeliveryChannel,
'AttributeList.member': parameters.Destination.AttributeList,
},
},
})

return GetSubscriptionResponse.decode(response).caseOf({
Right: (x) => [x.GetSubscriptionResponse.GetSubscriptionResult, meta],
Left: (error) => {
throw new ParsingError(error)
},
})
}

async createSubscription(parameters: CreateSubscriptionParameters): Promise<['', RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Subscriptions,
version: SUBSCRIPTIONS_API_VERSION,
action: 'SendTestNotificationToDestination',
action: 'CreateSubscription',
parameters: {
MarketplaceId: parameters.MarketplaceId,
Subscription: {
Expand Down
29 changes: 28 additions & 1 deletion test/unit/__snapshots__/subscriptions.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`sellers createSubscription returns the standard response if testing is succesful 1`] = `
exports[`sellers createSubscription returns the standard response if creation is succesful 1`] = `
Array [
"",
Object {
Expand Down Expand Up @@ -42,6 +42,33 @@ Array [
]
`;

exports[`sellers getSubscription returns a subscription if succesful 1`] = `
Array [
Object {
"Subscription": Object {
"Destination": Object {
"AttributeList": Array [
Object {
"Key": "sqsQueueUrl",
"Value": "https://sqs.us-east-1.amazonaws.com/51471EXAMPLE/mws_notifications",
},
],
"DeliveryChannel": "SQS",
},
"IsEnabled": true,
"NotificationType": "AnyOfferChanged",
},
},
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": 2020-04-06T10:22:23.582Z,
"requestId": "0",
"timestamp": 2020-05-06T09:22:23.582Z,
},
]
`;

exports[`sellers listRegisteredDestinations returns a list of destinations if succesful 1`] = `
Array [
Object {
Expand Down

0 comments on commit 54d8d20

Please sign in to comment.