@launchedla/snacktime
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -10,6 +10,5 @@ import { Action, BundleUpdate, RecordActionRequiredProps } from './types'; | ||
getActions(args: { | ||
store: string; | ||
bundleId: number; | ||
customerId: number; | ||
chargeId: number; | ||
bundleId?: number; | ||
chargeId?: number; | ||
}): Promise<Action[]>; | ||
@@ -16,0 +15,0 @@ }; |
@@ -24,10 +24,18 @@ "use strict"; | ||
async getActions(args) { | ||
let KeyConditionExpression = 'storeCustomer = :storeCustomer'; | ||
if (args.bundleId || args.chargeId) | ||
KeyConditionExpression += ' AND begins_with(bundleChargeTime, :bundleChargeTime)'; | ||
const ExpressionAttributeValues = { | ||
':storeCustomer': `${shopifyDomain}/${args.customerId}`, | ||
}; | ||
if (args.bundleId) { | ||
ExpressionAttributeValues[':bundleChargeTime'] = `${args.bundleId}/`; | ||
if (args.chargeId) | ||
ExpressionAttributeValues[':bundleChargeTime'] += `${args.chargeId}/`; | ||
} | ||
const res = await client | ||
.query({ | ||
TableName: actionsTableName, | ||
KeyConditionExpression: 'storeCustomer = :storeCustomer AND begins_with(bundleChargeTime, :bundleChargeTime)', | ||
ExpressionAttributeValues: { | ||
':storeCustomer': `${args.store}/${args.customerId}`, | ||
':bundleChargeTime': `${args.bundleId}/${args.chargeId}/`, | ||
}, | ||
KeyConditionExpression, | ||
ExpressionAttributeValues, | ||
}) | ||
@@ -34,0 +42,0 @@ .promise(); |
@@ -31,3 +31,3 @@ "use strict"; | ||
await snacktime.recordAction({ bundleId, customerId, chargeId }, { label: 'ChangedShipDate' }); | ||
const actions = await snacktime.getActions({ store: 'local-testing', bundleId, customerId, chargeId }); | ||
const actions = await snacktime.getActions({ bundleId, customerId, chargeId }); | ||
expect(actions[0].didExport).toBe(0); | ||
@@ -122,6 +122,16 @@ expect(actions[0].createdAt).toBeTruthy(); | ||
}); | ||
test('getActions', async () => { | ||
const actions = await snacktime.getActions({ store: 'local-testing', bundleId: 123, customerId: 123, chargeId: 123 }); | ||
expect(actions.length).toBeGreaterThan(0); | ||
describe('getActions', () => { | ||
test('for a customer', async () => { | ||
const actions = await snacktime.getActions({ customerId: 123 }); | ||
expect(actions.length).toBeGreaterThan(0); | ||
}); | ||
test('for a bundle', async () => { | ||
const actions = await snacktime.getActions({ customerId: 123, bundleId: 123 }); | ||
expect(actions.length).toBeGreaterThan(0); | ||
}); | ||
test('for a charge', async () => { | ||
const actions = await snacktime.getActions({ customerId: 123, bundleId: 123, chargeId: 123 }); | ||
expect(actions.length).toBeGreaterThan(0); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
{ | ||
"name": "@launchedla/snacktime", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Snacktime is a wrapper for the Launched LA internal database that will replicate into Snowflake. It is a replacement for Shopify metafields and an enhanced place to store event-driven data.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,71 +9,2 @@ # Snacktime | ||
``` | ||
import Snacktime from '@launched-la/snacktime' | ||
const snacktime = Snacktime({ | ||
awsRegion: 'us-east-2', | ||
shopifyDomain: 'hb-launched.myshopify.com', | ||
}) | ||
// bundle updates: | ||
await snacktime.recordAction({ | ||
label: 'UpdatedBundle', | ||
bundleId: 123, | ||
customerId: 123, | ||
chargeId: 123, | ||
updates: [ | ||
{ | ||
label: 'DeletedSubscription', | ||
subscriptionId: 123, | ||
variantId: 123, | ||
variantTitle: '2 / Sleepy Sloths' | ||
}, | ||
{ | ||
label: 'CreatedSubscription', | ||
subscriptionId: 123, | ||
variantId: 123, | ||
variantTitle: '3 / Sleepy Sloths' | ||
}, | ||
{ | ||
label: 'ChangedQuantity', | ||
subscriptionId: 123, | ||
variantId: 123, | ||
variantTitle: '3 / Spook-a-boo', | ||
oldQuantity: 1, | ||
newQuantity: 2, | ||
}, | ||
], | ||
}) | ||
// changing ship date: | ||
await snacktime.recordAction({ | ||
label: 'ChangedShipDate', | ||
bundleId: 123, | ||
customerId: 123, | ||
chargeId: 123, | ||
subscriptionIds: [123, 123, 123], | ||
oldDate: '2020-10-18', | ||
newDate: '2020-10-20', | ||
}) | ||
// cancellations: | ||
await snacktime.recordAction({ | ||
label: 'CancelledBundle', | ||
bundleId: 123, | ||
customerId: 123, | ||
chargeId: 123, | ||
reason: 'Too expensive', | ||
subscriptionIds: [123, 123, 123], | ||
}) | ||
// storing data | ||
// :value accepts number, string, object, and will store/parse intelligently | ||
// the data row will be created or updated, no need to specify | ||
await snacktime.saveData({ | ||
resource: 'Customer', | ||
resourceId: 123, | ||
namespace: 'snacks', | ||
key: 'numberOfChips', | ||
value: 5, | ||
}) | ||
``` | ||
There are 2 main functions: `recordAction` and `getActions`. Refer to [the test file](src/index.test.ts) for exact usage. |
@@ -32,3 +32,3 @@ import Snacktime from './' | ||
const actions = await snacktime.getActions({ store: 'local-testing', bundleId, customerId, chargeId }) | ||
const actions = await snacktime.getActions({ bundleId, customerId, chargeId }) | ||
expect(actions[0].didExport).toBe(0) | ||
@@ -142,5 +142,17 @@ expect(actions[0].createdAt).toBeTruthy() | ||
test('getActions', async () => { | ||
const actions = await snacktime.getActions({ store: 'local-testing', bundleId: 123, customerId: 123, chargeId: 123 }) | ||
expect(actions.length).toBeGreaterThan(0) | ||
describe('getActions', () => { | ||
test('for a customer', async () => { | ||
const actions = await snacktime.getActions({ customerId: 123 }) | ||
expect(actions.length).toBeGreaterThan(0) | ||
}) | ||
test('for a bundle', async () => { | ||
const actions = await snacktime.getActions({ customerId: 123, bundleId: 123 }) | ||
expect(actions.length).toBeGreaterThan(0) | ||
}) | ||
test('for a charge', async () => { | ||
const actions = await snacktime.getActions({ customerId: 123, bundleId: 123, chargeId: 123 }) | ||
expect(actions.length).toBeGreaterThan(0) | ||
}) | ||
}) |
@@ -36,16 +36,21 @@ import { DynamoDB } from 'aws-sdk' | ||
async getActions(args: { | ||
store: string | ||
bundleId: number | ||
customerId: number | ||
chargeId: number | ||
}): Promise<Action[]> { | ||
async getActions(args: { customerId: number; bundleId?: number; chargeId?: number }): Promise<Action[]> { | ||
let KeyConditionExpression = 'storeCustomer = :storeCustomer' | ||
if (args.bundleId || args.chargeId) | ||
KeyConditionExpression += ' AND begins_with(bundleChargeTime, :bundleChargeTime)' | ||
const ExpressionAttributeValues: { [key: string]: string } = { | ||
':storeCustomer': `${shopifyDomain}/${args.customerId}`, | ||
} | ||
if (args.bundleId) { | ||
ExpressionAttributeValues[':bundleChargeTime'] = `${args.bundleId}/` | ||
if (args.chargeId) ExpressionAttributeValues[':bundleChargeTime'] += `${args.chargeId}/` | ||
} | ||
const res = await client | ||
.query({ | ||
TableName: actionsTableName, | ||
KeyConditionExpression: 'storeCustomer = :storeCustomer AND begins_with(bundleChargeTime, :bundleChargeTime)', | ||
ExpressionAttributeValues: { | ||
':storeCustomer': `${args.store}/${args.customerId}`, | ||
':bundleChargeTime': `${args.bundleId}/${args.chargeId}/`, | ||
}, | ||
KeyConditionExpression, | ||
ExpressionAttributeValues, | ||
}) | ||
@@ -52,0 +57,0 @@ .promise() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1032125
967
10