From ec6529692185acb124fd4bdf5ff13cd16f60ba99 Mon Sep 17 00:00:00 2001 From: studna Date: Fri, 27 Nov 2020 15:00:32 +0100 Subject: [PATCH 1/6] Mock endpoints for usage --- services/billing/serverless.yaml | 22 +++++++++++++++++++ services/billing/src/usage.ts | 31 ++++++++++++++++++++++++++ services/billing/src/usageHistory.ts | 33 ++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 services/billing/src/usage.ts create mode 100644 services/billing/src/usageHistory.ts diff --git a/services/billing/serverless.yaml b/services/billing/serverless.yaml index d81adfb5..15d04606 100644 --- a/services/billing/serverless.yaml +++ b/services/billing/serverless.yaml @@ -86,4 +86,26 @@ functions: method: get path: /account cors: true + authorizer: arn:aws:lambda:${self:provider.region}:${ssm:org-arn}:function:space-rest-${self:provider.stage}-authorizer + + getUsage: + handler: dist/usage.handler + environment: + ENV: ${self:provider.stage} + events: + - http: + method: get + path: /account/usage + cors: true + authorizer: arn:aws:lambda:${self:provider.region}:${ssm:org-arn}:function:space-rest-${self:provider.stage}-authorizer + + getUsageHistory: + handler: dist/usageHistory.handler + environment: + ENV: ${self:provider.stage} + events: + - http: + method: get + path: /account/usage-history + cors: true authorizer: arn:aws:lambda:${self:provider.region}:${ssm:org-arn}:function:space-rest-${self:provider.stage}-authorizer \ No newline at end of file diff --git a/services/billing/src/usage.ts b/services/billing/src/usage.ts new file mode 100644 index 00000000..d051e109 --- /dev/null +++ b/services/billing/src/usage.ts @@ -0,0 +1,31 @@ +import { APIGatewayProxyResult, APIGatewayProxyEventBase } from 'aws-lambda'; +// import createDbModel from '@packages/models/dist/billing/dbModel'; + +if (!process?.env?.ENV) { + throw new Error('ENV variable not set'); +} + +// const STAGE = process.env.ENV; +// const dbModel = createDbModel(STAGE); + +export interface AuthContext { + uuid: string; + pubkey: string; +} + +// eslint-disable-next-line +export const handler = async ( + event: APIGatewayProxyEventBase +): Promise => { + const { uuid } = event.requestContext.authorizer; + + console.log(uuid); + + return { + statusCode: 200, + body: JSON.stringify({ + storage: Math.random() * 100000000, + bandwidth: Math.random() * 1000000, + }), + }; +}; diff --git a/services/billing/src/usageHistory.ts b/services/billing/src/usageHistory.ts new file mode 100644 index 00000000..86c70808 --- /dev/null +++ b/services/billing/src/usageHistory.ts @@ -0,0 +1,33 @@ +import { APIGatewayProxyResult, APIGatewayProxyEventBase } from 'aws-lambda'; +// import createDbModel from '@packages/models/dist/billing/dbModel'; + +if (!process?.env?.ENV) { + throw new Error('ENV variable not set'); +} + +// const STAGE = process.env.ENV; +// const dbModel = createDbModel(STAGE); + +export interface AuthContext { + uuid: string; + pubkey: string; +} + +// eslint-disable-next-line +export const handler = async ( + event: APIGatewayProxyEventBase +): Promise => { + const { uuid } = event.requestContext.authorizer; + + console.log(uuid); + + return { + statusCode: 200, + body: JSON.stringify([ + { + date: new Date().toISOString(), + storage: Math.random() * 100000000, + }, + ]), + }; +}; From afe2e846cf31f9232cb1e1de517521c437fe36fd Mon Sep 17 00:00:00 2001 From: studna Date: Tue, 1 Dec 2020 16:07:31 +0100 Subject: [PATCH 2/6] Add free usage quota per account --- packages/models/src/billing/ap/account.ts | 8 ++++++++ services/billing/src/usage.ts | 4 ++-- services/billing/src/usageHistory.ts | 18 ++++++++++++------ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/models/src/billing/ap/account.ts b/packages/models/src/billing/ap/account.ts index ae162387..971929cb 100644 --- a/packages/models/src/billing/ap/account.ts +++ b/packages/models/src/billing/ap/account.ts @@ -14,6 +14,11 @@ export enum BillingMode { STRIPE = 'stripe', } +const freeQuotas = { + [AccountPlan.BASIC]: 1000000000, // 1 GB + [AccountPlan.PRO]: 100000000000, // 100 GB +}; + export interface Account { id: string; credits: number; @@ -29,6 +34,7 @@ export interface Account { billingPeriodEnd?: string; stripeSubscriptionId?: string; billingMode?: BillingMode; + freeUsageQuota?: number; } interface RawInfo { @@ -45,6 +51,7 @@ interface RawInfo { billingPeriodEnd?: string; stripeSubscriptionId?: string; billingMode?: BillingMode; + freeUsageQuota?: number; } interface CreateAccountInput { @@ -90,6 +97,7 @@ const rawToObject = (raw: RawInfo): Account => { return { id: pk, ...rest, + freeUsageQuota: raw.freeUsageQuota || freeQuotas[raw.plan], }; }; diff --git a/services/billing/src/usage.ts b/services/billing/src/usage.ts index d051e109..a18b1b96 100644 --- a/services/billing/src/usage.ts +++ b/services/billing/src/usage.ts @@ -24,8 +24,8 @@ export const handler = async ( return { statusCode: 200, body: JSON.stringify({ - storage: Math.random() * 100000000, - bandwidth: Math.random() * 1000000, + storage: Math.ceil(Math.random() * 1000000000), + bandwidth: Math.ceil(Math.random() * 10000000), }), }; }; diff --git a/services/billing/src/usageHistory.ts b/services/billing/src/usageHistory.ts index 86c70808..6d5d82bf 100644 --- a/services/billing/src/usageHistory.ts +++ b/services/billing/src/usageHistory.ts @@ -21,13 +21,19 @@ export const handler = async ( console.log(uuid); + const mockData = []; + const now = Date.now(); + const day = 24 * 60 * 60 * 1000; + + for (let i = 0; i < 30; i += 1) { + mockData.push({ + date: new Date(now - i * day).toISOString(), + usage: Math.ceil(Math.random() * 1000000000), + }); + } + return { statusCode: 200, - body: JSON.stringify([ - { - date: new Date().toISOString(), - storage: Math.random() * 100000000, - }, - ]), + body: JSON.stringify(mockData), }; }; From e1bcc122ed8de0e0f215736b413c1af635538b44 Mon Sep 17 00:00:00 2001 From: studna Date: Tue, 1 Dec 2020 16:14:07 +0100 Subject: [PATCH 3/6] Add freeUsageQuota to account response --- services/billing/src/utils/responses.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/services/billing/src/utils/responses.ts b/services/billing/src/utils/responses.ts index 535e522b..8402ee8b 100644 --- a/services/billing/src/utils/responses.ts +++ b/services/billing/src/utils/responses.ts @@ -13,4 +13,5 @@ export const accountResponse = (accountWithBilling: AccountWithBilling) => 'billingPeriodStart', 'billingPeriodEnd', 'billingMode', + 'freeUsageQuota', ]); From 65355a0e389266e4f1f55e8501f4ec07f128cca0 Mon Sep 17 00:00:00 2001 From: studna Date: Wed, 2 Dec 2020 17:05:58 +0100 Subject: [PATCH 4/6] Use GiB instead of GB --- packages/models/src/billing/ap/account.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/models/src/billing/ap/account.ts b/packages/models/src/billing/ap/account.ts index 971929cb..66536b59 100644 --- a/packages/models/src/billing/ap/account.ts +++ b/packages/models/src/billing/ap/account.ts @@ -14,9 +14,11 @@ export enum BillingMode { STRIPE = 'stripe', } +const GiB = 1024 * 1024 * 1024; + const freeQuotas = { - [AccountPlan.BASIC]: 1000000000, // 1 GB - [AccountPlan.PRO]: 100000000000, // 100 GB + [AccountPlan.BASIC]: GiB, // 1 GiB + [AccountPlan.PRO]: 100 * GiB, // 100 GiB }; export interface Account { From a0d3843bd41fa4fe28f566de23ce7151f57d0e32 Mon Sep 17 00:00:00 2001 From: studna Date: Thu, 10 Dec 2020 18:43:33 +0100 Subject: [PATCH 5/6] draft --- services/billing/package.json | 2 + services/billing/src/usage.ts | 36 +++- yarn.lock | 306 +++++++++++++++++++++++++++++++++- 3 files changed, 339 insertions(+), 5 deletions(-) diff --git a/services/billing/package.json b/services/billing/package.json index f5c3721a..89d01231 100644 --- a/services/billing/package.json +++ b/services/billing/package.json @@ -19,6 +19,8 @@ "dependencies": { "@packages/apitools": "^0.0.1", "@packages/models": "^0.0.1", + "@textile/context": "^0.9.2", + "@textile/users": "^4.0.0", "@types/coinbase-commerce-node": "^1.0.5", "aws-lambda": "^1.0.6", "aws-sdk": "^2.775.0", diff --git a/services/billing/src/usage.ts b/services/billing/src/usage.ts index a18b1b96..bc6c7063 100644 --- a/services/billing/src/usage.ts +++ b/services/billing/src/usage.ts @@ -1,5 +1,6 @@ import { APIGatewayProxyResult, APIGatewayProxyEventBase } from 'aws-lambda'; -// import createDbModel from '@packages/models/dist/billing/dbModel'; +import { Context } from '@textile/context'; +import { Users } from '@textile/users'; if (!process?.env?.ENV) { throw new Error('ENV variable not set'); @@ -13,12 +14,43 @@ export interface AuthContext { pubkey: string; } +const { TestIdentity } = require('./id'); + +(async () => { + // set users key + // users.context.withAPIKey(TestIdentity.public.toString()); + + console.log(users.context); + try { + console.log('usage', JSON.stringify(usage.customer)); + } catch (e) { + console.log(e); + } +})(); + +const ctx = new Context('http://52.11.247.86:3007'); + // eslint-disable-next-line export const handler = async ( event: APIGatewayProxyEventBase ): Promise => { - const { uuid } = event.requestContext.authorizer; + const { uuid, pubkey } = event.requestContext.authorizer; + + try { + const users = await Users.withKeyInfo( + { + key: 'bwmvr74ev3sqmnj5hchooa2zkzm', + secret: 'byiyrna6l3wnfdp4a7aaxsiz573jrtuvrz3qhj6a', + }, + ctx + ); + const usage = await users.getUsage({ + dependentUserKey: TestIdentity.public.toString(), + }); + } catch (e) { + // zero usage + } console.log(uuid); return { diff --git a/yarn.lock b/yarn.lock index dcc4ef79..8fe49dd7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2855,6 +2855,11 @@ call-me-maybe "^1.0.1" glob-to-regexp "^0.3.0" +"@multiformats/base-x@^4.0.1": + version "4.0.1" + resolved "https://registry.yarnpkg.com/@multiformats/base-x/-/base-x-4.0.1.tgz#95ff0fa58711789d53aefb2590a8b7a4e715d121" + integrity sha512-eMk0b9ReBbV23xXU693TAIrLyeO5iTgBZGSJfpqriG8UkYvr/hC9u9pyMlAakDNHWmbhMZCDs6KQO0jzKD8OTw== + "@nodelib/fs.scandir@2.1.3": version "2.1.3" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b" @@ -3205,6 +3210,15 @@ dependencies: defer-to-connect "^2.0.0" +"@textile/buckets-grpc@2.2.2": + version "2.2.2" + resolved "https://registry.yarnpkg.com/@textile/buckets-grpc/-/buckets-grpc-2.2.2.tgz#c01ba950a5f148040c111e3124d6dcbfbaba5638" + integrity sha512-MTc6fFx9dsFD2+HkzxPsxxiYCw2FQ8Dm0QuCx445n1eFPcPMBTMbRoc+7imLieXQGEfnEDSp1wHt+RqDQ8y0rw== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@types/google-protobuf" "^3.7.4" + google-protobuf "^3.13.0" + "@textile/buckets-grpc@^1.0.14": version "1.0.14" resolved "https://registry.yarnpkg.com/@textile/buckets-grpc/-/buckets-grpc-1.0.14.tgz#08ec8d2c0e49f7141f892e424ad41fa58b7ff877" @@ -3243,6 +3257,14 @@ dependencies: "@textile/security" "^0.2.6" +"@textile/context@^0.9.2": + version "0.9.2" + resolved "https://registry.yarnpkg.com/@textile/context/-/context-0.9.2.tgz#3d6e3e92bca013b469a1da772839817afbe28f53" + integrity sha512-88rn/uBikldDq5eA33qnGT6oBnXKW/cxWVUfFxlzLDMj4NvcifpplAdac4/hcJhK+i9CubO/WHMGfI6/qQgW4A== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@textile/security" "^0.6.2" + "@textile/crypto@^0.1.0": version "0.1.0" resolved "https://registry.yarnpkg.com/@textile/crypto/-/crypto-0.1.0.tgz#f1b57330e1a2a2845b6bc625000939dd94b5d8ce" @@ -3253,6 +3275,28 @@ multibase "^3.0.0" tweetnacl "^1.0.3" +"@textile/crypto@^0.1.2": + version "0.1.2" + resolved "https://registry.yarnpkg.com/@textile/crypto/-/crypto-0.1.2.tgz#09637e8173daa8d62a78f570e170065ac631b089" + integrity sha512-vHmxLlTfPliq1LgaTwih5h9YPCJf5zotas2ld2TX8S6uOYY2jRoLTPR60ddAMfDxPmtuXvFaPz/aRJsHuqgwUA== + dependencies: + "@types/ed2curve" "^0.2.2" + "@types/multibase" "^0.6.0" + ed2curve "^0.3.0" + multibase "^3.0.0" + tweetnacl "^1.0.3" + +"@textile/crypto@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@textile/crypto/-/crypto-2.0.0.tgz#63c253656a87a0064764b12a262039c921605f18" + integrity sha512-A0f6rRuLC3/MM0EhfYXK9prUckiRUNDsv2Gemjqpr8KhAmDM0lCmnVXwsBxAS6qHusLh8GftjfdKMd/ymvnNDw== + dependencies: + "@types/ed2curve" "^0.2.2" + "@types/multibase" "^0.6.0" + ed2curve "^0.3.0" + multibase "^3.1.0" + tweetnacl "^1.0.3" + "@textile/grpc-authentication@^0.1.4": version "0.1.4" resolved "https://registry.yarnpkg.com/@textile/grpc-authentication/-/grpc-authentication-0.1.4.tgz#051cba27560a09b715b68d7fd446e531a62ee3f3" @@ -3264,6 +3308,17 @@ "@textile/hub-threads-client" "^0.4.4" "@textile/security" "^0.2.6" +"@textile/grpc-authentication@^2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@textile/grpc-authentication/-/grpc-authentication-2.1.0.tgz#fa921df5e16f95154046d4735d5dd571f7c381f1" + integrity sha512-XzY882vqQdrD8eNn/iaaT72/2M+1ouZZG5IOANkvrzlL3VnY2Lk0E3NjbVpIAggjxYzppFY0POwIVxPwu2OuGg== + dependencies: + "@textile/context" "^0.9.2" + "@textile/crypto" "^2.0.0" + "@textile/grpc-connection" "^2.1.0" + "@textile/hub-threads-client" "^4.0.0" + "@textile/security" "^0.6.2" + "@textile/grpc-connection@^0.1.2": version "0.1.2" resolved "https://registry.yarnpkg.com/@textile/grpc-connection/-/grpc-connection-0.1.2.tgz#67d77e9d82f0713e59fad10effcebe1182bcec5c" @@ -3273,6 +3328,15 @@ "@textile/context" "^0.6.7" "@textile/grpc-transport" "^0.0.3" +"@textile/grpc-connection@^2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@textile/grpc-connection/-/grpc-connection-2.1.0.tgz#6b1cd731f842cd107c6356ecee5ce2485e431471" + integrity sha512-rGDLqA6feV7DFr27eAeRcESCh1Nik1OVbkVpGUTZ0j1oE3J0OH/wR6aSw1w3Cs0gfPOTGp12aZkGGuJgQIBp7A== + dependencies: + "@improbable-eng/grpc-web" "^0.12.0" + "@textile/context" "^0.9.2" + "@textile/grpc-transport" "^0.2.1" + "@textile/grpc-transport@^0.0.3": version "0.0.3" resolved "https://registry.yarnpkg.com/@textile/grpc-transport/-/grpc-transport-0.0.3.tgz#ab094b7637da8188d85b1eb1a728ae45d7dc9e5f" @@ -3284,6 +3348,26 @@ loglevel "^1.6.6" ws "^7.2.1" +"@textile/grpc-transport@^0.2.1": + version "0.2.1" + resolved "https://registry.yarnpkg.com/@textile/grpc-transport/-/grpc-transport-0.2.1.tgz#b5c79c48e3f6ac859736145f88e4ff6b5273353f" + integrity sha512-/2GhUiygoKmUNZC1x9Ev4wb4RbeEfPCLKqK1R2Vo2ATsf2ft6A4j7sURVk2JdoIQ9O6glKB/yDbUqrKHAhX2ig== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@types/ws" "^7.2.6" + isomorphic-ws "^4.0.1" + loglevel "^1.6.6" + ws "^7.2.1" + +"@textile/hub-grpc@2.2.2": + version "2.2.2" + resolved "https://registry.yarnpkg.com/@textile/hub-grpc/-/hub-grpc-2.2.2.tgz#ce10369fae9c9eb2c89e0b55033e2b58ca5f6187" + integrity sha512-N9U6ujMJLRgg/JNbRl7K072NuEWRQRiTnW2eGdz0K/rGLQhUYF4c2mi70DUVcT/SHND8KUl9lcdN9/yoiQubkw== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@types/google-protobuf" "^3.7.4" + google-protobuf "^3.13.0" + "@textile/hub-threads-client@^0.4.4": version "0.4.4" resolved "https://registry.yarnpkg.com/@textile/hub-threads-client/-/hub-threads-client-0.4.4.tgz#5d00e83db616354474433e7dff945780c46f16f6" @@ -3299,6 +3383,20 @@ loglevel "^1.6.8" multihashes "0.4.19" +"@textile/hub-threads-client@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@textile/hub-threads-client/-/hub-threads-client-4.0.0.tgz#ec3387001b0c9fb239515fd3d31dc37205753136" + integrity sha512-YYE99H9JT36zUclQ3Pg9PZyYaz1QzMdy5dUJrdmYLoiqPqKmC/YGR+rPa6wVsMeGWN+vfrxCi8DT32VVl1U5lg== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@textile/context" "^0.9.2" + "@textile/hub-grpc" "2.2.2" + "@textile/security" "^0.6.2" + "@textile/threads-client" "^1.3.2" + "@textile/threads-id" "^0.3.1" + "@textile/users-grpc" "2.2.2" + loglevel "^1.7.0" + "@textile/hub@^0.6.1": version "0.6.2" resolved "https://registry.yarnpkg.com/@textile/hub/-/hub-0.6.2.tgz#0be6bbfa32a10ada11fa730b4debd9ecc968a0e0" @@ -3330,6 +3428,16 @@ multiaddr "^7.4.2" varint "^5.0.0" +"@textile/multiaddr@^0.3.1": + version "0.3.1" + resolved "https://registry.yarnpkg.com/@textile/multiaddr/-/multiaddr-0.3.1.tgz#0c34678b0afac58abfc5b9efab829aa66cde9af9" + integrity sha512-vCYLuBqxVo1ki0qNRV7ms1uux1ojjCJwCEb/jcVpFQH1zt7AO73/VIWTjtsLvZj4MuZ8ut2mIjNbJfP6Rl6Slg== + dependencies: + "@textile/threads-id" "^0.3.1" + multiaddr "^8.1.1" + uint8arrays "^1.1.0" + varint "^6.0.0" + "@textile/security@^0.2.6": version "0.2.6" resolved "https://registry.yarnpkg.com/@textile/security/-/security-0.2.6.tgz#ef0670abe92f41efd50c69a295d65a82f5521e2b" @@ -3339,6 +3447,15 @@ fast-sha256 "^1.3.0" multibase "^1.0.0" +"@textile/security@^0.6.2": + version "0.6.2" + resolved "https://registry.yarnpkg.com/@textile/security/-/security-0.6.2.tgz#a7ae385bac715dcbd68d66f352b847e6d335876e" + integrity sha512-RcDaR+S0kk6XepE0IF3JfdyRJ3fyEI1fxrWZxYTXmmfMihhhQWLeF1PhEvsUsiou32KBk6UjpxmhiNcqjIVwWg== + dependencies: + "@consento/sync-randombytes" "^1.0.5" + fast-sha256 "^1.3.0" + multibase "^3.1.0" + "@textile/threads-client-grpc@^0.1.23": version "0.1.23" resolved "https://registry.yarnpkg.com/@textile/threads-client-grpc/-/threads-client-grpc-0.1.23.tgz#5e87db32c6d7ab503b99c1ecdd7be04d139df1a8" @@ -3348,6 +3465,15 @@ "@types/google-protobuf" "^3.7.2" google-protobuf "^3.12.2" +"@textile/threads-client-grpc@^1.0.1": + version "1.0.2" + resolved "https://registry.yarnpkg.com/@textile/threads-client-grpc/-/threads-client-grpc-1.0.2.tgz#5d6ee09431eef2eb582f116bb3b48698e9fedc99" + integrity sha512-yrgdUb3VLGW18HKmbzAU8L7NElhnPYKWG9cHZG6EnV3ITS9zOiDydfVSNSkojEDfoFSel5x3eAUiOQbXUrkKng== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@types/google-protobuf" "^3.7.3" + google-protobuf "^3.13.0" + "@textile/threads-client@^0.10.1": version "0.10.1" resolved "https://registry.yarnpkg.com/@textile/threads-client/-/threads-client-0.10.1.tgz#f5fb6067fede0579d7ec7c632ac2f78815b7c559" @@ -3368,6 +3494,22 @@ google-protobuf "^3.10.0" to-json-schema "^0.2.5" +"@textile/threads-client@^1.3.2": + version "1.3.2" + resolved "https://registry.yarnpkg.com/@textile/threads-client/-/threads-client-1.3.2.tgz#3d89fa378c9029cc2bee40d01f40cebb726bfabc" + integrity sha512-4j8zcwzvv1TQX3DMvl7UNM0X+aJ3gPhXEcPTdhqv71/5bQ4PAancwkmAf3N7JN9eeG/u0U8miSUHLKTD2kpznw== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@textile/context" "^0.9.2" + "@textile/crypto" "^0.1.2" + "@textile/grpc-transport" "^0.2.1" + "@textile/multiaddr" "^0.3.1" + "@textile/security" "^0.6.2" + "@textile/threads-client-grpc" "^1.0.1" + "@textile/threads-id" "^0.3.1" + "@types/to-json-schema" "^0.2.0" + to-json-schema "^0.2.5" + "@textile/threads-core@^0.3.0": version "0.3.1" resolved "https://registry.yarnpkg.com/@textile/threads-core/-/threads-core-0.3.1.tgz#43f95b9e6f364e64911e458bdb7e8c81dfd1a570" @@ -3411,6 +3553,24 @@ multibase "^1.0.1" varint "^5.0.0" +"@textile/threads-id@^0.3.1": + version "0.3.1" + resolved "https://registry.yarnpkg.com/@textile/threads-id/-/threads-id-0.3.1.tgz#0149ae7a1c520a5d8f933772e2dc00913d84712a" + integrity sha512-Asxpu9g9EDbKoefP3E3bvzkPj/Uft7TwhiPispYeYdwktO82kCtCk85XUaW5Fyn4/z2h5tzC2XaTCYSFabv2pA== + dependencies: + "@consento/sync-randombytes" "^1.0.4" + multibase "^3.1.0" + varint "^6.0.0" + +"@textile/users-grpc@2.2.2": + version "2.2.2" + resolved "https://registry.yarnpkg.com/@textile/users-grpc/-/users-grpc-2.2.2.tgz#e8b1146474b3dec965761ee81cfc98a1c14c16dd" + integrity sha512-NC7NAb2VE34HtVAUEnSzl2R/haWgKdc/JQ1RmzdhPLU+9XYrNiq8NljIn00YQaLixo5V0T3kC8iSxsbbUIK20Q== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@types/google-protobuf" "^3.7.4" + google-protobuf "^3.13.0" + "@textile/users-grpc@^1.0.14": version "1.0.14" resolved "https://registry.yarnpkg.com/@textile/users-grpc/-/users-grpc-1.0.14.tgz#37de8f213988b79b77c0901e35074f33621f9298" @@ -3442,6 +3602,26 @@ loglevel "^1.6.8" next-tick "^1.1.0" +"@textile/users@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@textile/users/-/users-4.0.0.tgz#c428444511b3d67c9c0662826e1686842b443e8d" + integrity sha512-TIoK2qmVT5jbOlEKQERKy5EYDAvEnjVYXn2KlKdE0+QaHnwY+LCrXroVfFmJ/8GTZIXwJqRjWAqZ/HHKZ5hTrA== + dependencies: + "@improbable-eng/grpc-web" "^0.13.0" + "@textile/buckets-grpc" "2.2.2" + "@textile/context" "^0.9.2" + "@textile/crypto" "^2.0.0" + "@textile/grpc-authentication" "^2.1.0" + "@textile/grpc-connection" "^2.1.0" + "@textile/grpc-transport" "^0.2.1" + "@textile/hub-grpc" "2.2.2" + "@textile/hub-threads-client" "^4.0.0" + "@textile/security" "^0.6.2" + "@textile/threads-id" "^0.3.1" + "@textile/users-grpc" "2.2.2" + event-iterator "^2.0.0" + loglevel "^1.7.0" + "@turist/fetch@^7.1.7": version "7.1.7" resolved "https://registry.yarnpkg.com/@turist/fetch/-/fetch-7.1.7.tgz#a2b1f7ec0265e6fe0946c51eef34bad9b9efc865" @@ -3590,6 +3770,11 @@ resolved "https://registry.yarnpkg.com/@types/google-protobuf/-/google-protobuf-3.7.3.tgz#429512e541bbd777f2c867692e6335ee08d1f6d4" integrity sha512-FRwj40euE2bYkG+0X5w2nEA8yAzgJRcEa7RBd0Gsdkb9/tPM2pctVVAvnOUTbcXo2VmIHPo0Ae94Gl9vRHfKzg== +"@types/google-protobuf@^3.7.3", "@types/google-protobuf@^3.7.4": + version "3.7.4" + resolved "https://registry.yarnpkg.com/@types/google-protobuf/-/google-protobuf-3.7.4.tgz#1621c50ceaf5aefa699851da8e0ea606a2943a39" + integrity sha512-6PjMFKl13cgB4kRdYtvyjKl8VVa0PXS2IdVxHhQ8GEKbxBkyJtSbaIeK1eZGjDKN7dvUh4vkOvU9FMwYNv4GQQ== + "@types/hast@^2.0.0": version "2.3.1" resolved "https://registry.yarnpkg.com/@types/hast/-/hast-2.3.1.tgz#b16872f2a6144c7025f296fb9636a667ebb79cd9" @@ -5970,6 +6155,17 @@ cids@^0.8.0, cids@^0.8.3, cids@~0.8.0, cids@~0.8.3: multicodec "^1.0.1" multihashes "^1.0.1" +cids@^1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/cids/-/cids-1.0.2.tgz#04ebadd65e5600a07feb16db594160f087c6eab3" + integrity sha512-ohCcYyEHh0Z5Hl+O1IML4kt6Kx5GPho1ybxtqK4zyk6DeV5CvOLoT/mqDh0cgKcAvsls3vcVa9HjZc7RQr3geA== + dependencies: + class-is "^1.1.0" + multibase "^3.0.1" + multicodec "^2.0.1" + multihashes "^3.0.1" + uint8arrays "^1.1.0" + cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3: version "1.0.4" resolved "https://registry.yarnpkg.com/cipher-base/-/cipher-base-1.0.4.tgz#8760e4ecc272f4c363532f926d874aae2c1397de" @@ -7027,6 +7223,13 @@ debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@~4.1.0: dependencies: ms "^2.1.1" +debug@^4.2.0: + version "4.3.1" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee" + integrity sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ== + dependencies: + ms "2.1.2" + debuglog@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492" @@ -7440,6 +7643,15 @@ dns-equal@^1.0.0: resolved "https://registry.yarnpkg.com/dns-equal/-/dns-equal-1.0.0.tgz#b39e7f1da6eb0a75ba9c17324b34753c47e0654d" integrity sha1-s55/HabrCnW6nBcySzR1PEfgZU0= +dns-over-http-resolver@^1.0.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/dns-over-http-resolver/-/dns-over-http-resolver-1.2.0.tgz#1f9c808c88810b9ffe5c4a6ece449764e9658002" + integrity sha512-LJ1sEbQgwY+qmL6z3kNIKi0vHA9nSUdZb8vf3G6z43ZVIF6WhhNHXztLMOOvaMIvtCsCZBjAie11MtUD3+H0YA== + dependencies: + debug "^4.2.0" + native-fetch "^2.0.1" + receptacle "^1.3.2" + dns-packet@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/dns-packet/-/dns-packet-1.3.1.tgz#12aa426981075be500b910eedcd0b47dd7deda5a" @@ -7823,7 +8035,7 @@ err-code@^1.0.0: resolved "https://registry.yarnpkg.com/err-code/-/err-code-1.1.2.tgz#06e0116d3028f6aef4806849eb0ea6a748ae6960" integrity sha1-BuARbTAo9q70gGhJ6w6mp0iuaWA= -err-code@^2.0.0: +err-code@^2.0.0, err-code@^2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/err-code/-/err-code-2.0.3.tgz#23c2f3b756ffdfc608d30e27c9a941024807e7f9" integrity sha512-2bmlRpNKBxT/CRmPOlyISQpNj+qSeYvcym/uT0Jx2bMOlKLtSy1ZmLuVxSEKKyor/N5yhvp/ZiG1oE3DEYMSFA== @@ -9969,6 +10181,13 @@ globals@^12.1.0: dependencies: type-fest "^0.8.1" +globalthis@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/globalthis/-/globalthis-1.0.1.tgz#40116f5d9c071f9e8fb0037654df1ab3a83b7ef9" + integrity sha512-mJPRTc/P39NH/iNG4mXa9aIhNymaQikTrnspeCa2ZuJ+mH2QN/rXwtX3XwKrHqWgUQFbNZKtHM105aHzJalElw== + dependencies: + define-properties "^1.1.3" + globby@^10.0.0, globby@^10.0.1: version "10.0.2" resolved "https://registry.yarnpkg.com/globby/-/globby-10.0.2.tgz#277593e745acaa4646c3ab411289ec47a0392543" @@ -10045,6 +10264,11 @@ google-protobuf@3.13.0, google-protobuf@^3.10.0, google-protobuf@^3.12.1, google resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.13.0.tgz#909c5983d75dd6101ed57c79e0528d000cdc3251" integrity sha512-ZIf3qfLFayVrPvAjeKKxO5FRF1/NwRxt6Dko+fWEMuHwHbZx8/fcaAao9b0wCM6kr8qeg2te8XTpyuvKuD9aKw== +google-protobuf@^3.13.0: + version "3.14.0" + resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.14.0.tgz#20373d22046e63831a5110e11a84f713cc43651e" + integrity sha512-bwa8dBuMpOxg7COyqkW6muQuvNnWgVN8TX/epDRGW5m0jcrmq2QJyCyiV8ZE2/6LaIIqJtiv9bYokFhfpy/o6w== + got@8.3.2, got@^8.3.1: version "8.3.2" resolved "https://registry.yarnpkg.com/got/-/got-8.3.2.tgz#1d23f64390e97f776cac52e5b936e5f514d2e937" @@ -13065,6 +13289,11 @@ loglevel@^1.6.6, loglevel@^1.6.8: resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.7.0.tgz#728166855a740d59d38db01cf46f042caa041bb0" integrity sha512-i2sY04nal5jDcagM3FMfG++T69GEEM8CYuOfeOIvmXzOIcwE9a/CJPR0MFM97pYMj/u10lzz7/zd7+qwhrBTqQ== +loglevel@^1.7.0: + version "1.7.1" + resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.7.1.tgz#005fde2f5e6e47068f935ff28573e125ef72f197" + integrity sha512-Hesni4s5UkWkwCGJMQGAh71PaLUmKFM60dHvq0zi/vDhhrzuk+4GgNbTXJ12YYQJn6ZKBDNIjYcuQGKudvqrIw== + longest-streak@^2.0.1: version "2.0.4" resolved "https://registry.yarnpkg.com/longest-streak/-/longest-streak-2.0.4.tgz#b8599957da5b5dab64dee3fe316fa774597d90e4" @@ -13726,7 +13955,7 @@ ms@2.1.1: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a" integrity sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg== -ms@^2.0.0, ms@^2.1.1: +ms@2.1.2, ms@^2.0.0, ms@^2.1.1: version "2.1.2" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== @@ -13750,6 +13979,20 @@ multiaddr@^7.2.1, multiaddr@^7.4.2, multiaddr@^7.4.3: multibase "^0.7.0" varint "^5.0.0" +multiaddr@^8.1.1: + version "8.1.1" + resolved "https://registry.yarnpkg.com/multiaddr/-/multiaddr-8.1.1.tgz#d86315deed93752fa68b4199fee56f8b4a516a2b" + integrity sha512-Dyur7rWX44MlgKIqVA2dYPOZx/UwG60PVSffJ5S17uo6Pu31lftJXShMEfPtUDGHnyALAOWOuC3X/iPhDtw4Vg== + dependencies: + cids "^1.0.0" + class-is "^1.1.0" + dns-over-http-resolver "^1.0.0" + err-code "^2.0.3" + is-ip "^3.1.0" + multibase "^3.0.0" + uint8arrays "^1.1.0" + varint "^5.0.0" + multibase@^0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/multibase/-/multibase-0.7.0.tgz#1adfc1c50abe05eefeb5091ac0c2728d6b84581b" @@ -13774,6 +14017,14 @@ multibase@^3.0.0: base-x "^3.0.8" web-encoding "^1.0.2" +multibase@^3.0.1, multibase@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/multibase/-/multibase-3.1.0.tgz#2999aebbd34b3fa85cd5a9d42babc6031aeccafe" + integrity sha512-Z+pThrpbS7ckQ2DwW5mPiwCGe1a94f8DWi/OxmbyeRednVOyUKmLSE+60kL/WHFYwWnaD1OakXGk3PYI1NkMFw== + dependencies: + "@multiformats/base-x" "^4.0.1" + web-encoding "^1.0.4" + multicast-dns-service-types@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/multicast-dns-service-types/-/multicast-dns-service-types-1.1.0.tgz#899f11d9686e5e05cb91b35d5f0e63b773cfc901" @@ -13795,6 +14046,14 @@ multicodec@^1.0.0, multicodec@^1.0.1, multicodec@^1.0.3, multicodec@^1.0.4: buffer "^5.6.0" varint "^5.0.0" +multicodec@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/multicodec/-/multicodec-2.0.1.tgz#0971bbef83fcb354315c837c9a3f3e2e422af371" + integrity sha512-YDYeWn9iGa76hOHAyyZa0kbt3tr5FLg1ZXUHrZUJltjnxxdbTIbHnxWLd2zTcMOjdT3QyO+Xs4bQgJUcC2RWUA== + dependencies: + uint8arrays "1.0.0" + varint "^5.0.0" + multihashes@0.4.19: version "0.4.19" resolved "https://registry.yarnpkg.com/multihashes/-/multihashes-0.4.19.tgz#d7493cf028e48747122f350908ea13d12d204813" @@ -13813,6 +14072,15 @@ multihashes@^1.0.1: multibase "^1.0.1" varint "^5.0.0" +multihashes@^3.0.1: + version "3.1.0" + resolved "https://registry.yarnpkg.com/multihashes/-/multihashes-3.1.0.tgz#aada18fc55169ae044c3455e2264024bd00f5690" + integrity sha512-snU+w6aZy5bTrrqIHW3wkT0MfHmxcpOsaVNJt0NzUnseksbjFDVUZjSmhDMAVOVnIdLMS7xHjo55pKlBIGmC3g== + dependencies: + multibase "^3.1.0" + uint8arrays "^1.0.0" + varint "^6.0.0" + multihashing-async@^0.8.1, multihashing-async@~0.8.1: version "0.8.2" resolved "https://registry.yarnpkg.com/multihashing-async/-/multihashing-async-0.8.2.tgz#3d5da05df27d83be923f6d04143a0954ff87f27f" @@ -13913,6 +14181,13 @@ napi-build-utils@^1.0.1: resolved "https://registry.yarnpkg.com/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806" integrity sha512-ONmRUqK7zj7DWX0D9ADe03wbwOBZxNAfF20PlGfCWQcD3+/MakShIHrMqx9YwPTfxDdF1zLeL+RGZiR9kGMLdg== +native-fetch@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/native-fetch/-/native-fetch-2.0.1.tgz#319d53741a7040def92d5dc8ea5fe9416b1fad89" + integrity sha512-gv4Bea+ga9QdXINurpkEqun3ap3vnB+WYoe4c8ddqUYEH7B2h6iD39RF8uVN7OwmSfMY3RDxkvBnoI4e2/vLXQ== + dependencies: + globalthis "^1.0.1" + native-url@^0.2.6: version "0.2.6" resolved "https://registry.yarnpkg.com/native-url/-/native-url-0.2.6.tgz#ca1258f5ace169c716ff44eccbddb674e10399ae" @@ -16378,6 +16653,13 @@ realpath-native@^1.1.0: dependencies: util.promisify "^1.0.0" +receptacle@^1.3.2: + version "1.3.2" + resolved "https://registry.yarnpkg.com/receptacle/-/receptacle-1.3.2.tgz#a7994c7efafc7a01d0e2041839dab6c4951360d2" + integrity sha512-HrsFvqZZheusncQRiEE7GatOAETrARKV/lnfYicIm8lbvp/JQOdADOfhjBd2DajvoszEyxSM6RlAAIZgEoeu/A== + dependencies: + ms "^2.1.1" + recursive-readdir@2.2.1: version "2.2.1" resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.1.tgz#90ef231d0778c5ce093c9a48d74e5c5422d13a99" @@ -18806,7 +19088,15 @@ uid-number@0.0.6: resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81" integrity sha1-DqEOgDXo61uOREnwbaHHMGY7qoE= -uint8arrays@^1.0.0: +uint8arrays@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/uint8arrays/-/uint8arrays-1.0.0.tgz#9cf979517f85c32d6ef54adf824e3499bb715331" + integrity sha512-14tqEVujDREW7YwonSZZwLvo7aFDfX7b6ubvM/U7XvZol+CC/LbhaX/550VlWmhddAL9Wou1sxp0Of3tGqXigg== + dependencies: + multibase "^3.0.0" + web-encoding "^1.0.2" + +uint8arrays@^1.0.0, uint8arrays@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/uint8arrays/-/uint8arrays-1.1.0.tgz#d034aa65399a9fd213a1579e323f0b29f67d0ed2" integrity sha512-cLdlZ6jnFczsKf5IH1gPHTtcHtPGho5r4CvctohmQjw8K7Q3gFdfIGHxSTdTaCKrL4w09SsPRJTqRS0drYeszA== @@ -19281,6 +19571,11 @@ varint@^5.0.0, varint@~5.0.0: resolved "https://registry.yarnpkg.com/varint/-/varint-5.0.0.tgz#d826b89f7490732fabc0c0ed693ed475dcb29ebf" integrity sha1-2Ca4n3SQcy+rwMDtaT7Uddyynr8= +varint@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/varint/-/varint-6.0.0.tgz#9881eb0ce8feaea6512439d19ddf84bf551661d0" + integrity sha512-cXEIW6cfr15lFv563k4GuVuW/fiwjknytD37jIOLSdSWuOI6WnO/oKwmP2FQTU2l01LP8/M5TSAJpzUaGe3uWg== + vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" @@ -19400,6 +19695,11 @@ web-encoding@^1.0.2: resolved "https://registry.yarnpkg.com/web-encoding/-/web-encoding-1.0.3.tgz#f96a7198d023be50f786fe9070eae360f4c1d77f" integrity sha512-Ajn64qJ0Z3oMwOIwBtxajFPA+4guB12n4EfmY1Mtlgb9296WJxwH1q/ykedmQrBNpjcKCM207S5OM2wpJfl4VA== +web-encoding@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/web-encoding/-/web-encoding-1.0.4.tgz#0398d39ce2cbef5ed2617080750ed874e6153aea" + integrity sha512-DcXs2lbVPzuJmn2kuDEwul2oZg7p4YMa5J2f0YzsOBHaAnBYGPNUB/rJ74DTjTKpw7F0+lSsVM8sFHE2UyBixg== + web-namespaces@^1.0.0: version "1.1.4" resolved "https://registry.yarnpkg.com/web-namespaces/-/web-namespaces-1.1.4.tgz#bc98a3de60dadd7faefc403d1076d529f5e030ec" From f196eed43b0baae74bbeb5b8993dba87fb61cbe0 Mon Sep 17 00:00:00 2001 From: studna Date: Mon, 28 Dec 2020 16:31:33 +0100 Subject: [PATCH 6/6] UpdatE --- services/billing/package.json | 1 + services/billing/serverless.yaml | 21 +++++++---- services/billing/src/usage.ts | 46 +++++++----------------- services/billing/src/utils/usage.ts | 55 +++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 services/billing/src/utils/usage.ts diff --git a/services/billing/package.json b/services/billing/package.json index 89d01231..6a52916b 100644 --- a/services/billing/package.json +++ b/services/billing/package.json @@ -26,6 +26,7 @@ "aws-sdk": "^2.775.0", "coinbase-commerce-node": "^1.0.4", "lodash": "^4.17.20", + "multibase": "^3.1.0", "stripe": "^8.97.0" }, "devDependencies": { diff --git a/services/billing/serverless.yaml b/services/billing/serverless.yaml index 15d04606..e575a9ad 100644 --- a/services/billing/serverless.yaml +++ b/services/billing/serverless.yaml @@ -17,27 +17,28 @@ provider: Resource: - Fn::ImportValue: 'AppDynamoDbTableArn-${self:provider.stage}' - Fn::Join: - - "" - - - Fn::ImportValue: 'AppDynamoDbTableArn-${self:provider.stage}' - - "/*" + - '' + - - Fn::ImportValue: 'AppDynamoDbTableArn-${self:provider.stage}' + - '/*' - Effect: Allow Action: - execute-api:ManageConnections Resource: - - "arn:aws:execute-api:${self:provider.region}:${ssm:org-arn}:${self:provider.executeApi.${self:provider.stage}}/${opt:stage}/*" + - 'arn:aws:execute-api:${self:provider.region}:${ssm:org-arn}:${self:provider.executeApi.${self:provider.stage}}/${opt:stage}/*' executeApi: dev: 413h5xnqp8 + prd: 413h5xnqp8 plugins: - serverless-jetpack custom: jetpack: - base: "../../" + base: '../../' package: exclude: - - "**/node_modules/aws-sdk/**" # included on Lambda. + - '**/node_modules/aws-sdk/**' # included on Lambda. functions: createStripeSubscription: @@ -92,6 +93,9 @@ functions: handler: dist/usage.handler environment: ENV: ${self:provider.stage} + TXL_USER_KEY: ${ssm:txl-user-key-${self:provider.stage}~true} + TXL_USER_SECRET: ${ssm:txl-user-secret-${self:provider.stage}~true} + TXL_HUB_URL: ${ssm:txl-hub-url-${self:provider.stage}~true} events: - http: method: get @@ -103,9 +107,12 @@ functions: handler: dist/usageHistory.handler environment: ENV: ${self:provider.stage} + TXL_USER_KEY: ${ssm:txl-user-key-${self:provider.stage}~true} + TXL_USER_SECRET: ${ssm:txl-user-secret-${self:provider.stage}~true} + TXL_HUB_URL: ${ssm:txl-hub-url-${self:provider.stage}~true} events: - http: method: get path: /account/usage-history cors: true - authorizer: arn:aws:lambda:${self:provider.region}:${ssm:org-arn}:function:space-rest-${self:provider.stage}-authorizer \ No newline at end of file + authorizer: arn:aws:lambda:${self:provider.region}:${ssm:org-arn}:function:space-rest-${self:provider.stage}-authorizer diff --git a/services/billing/src/usage.ts b/services/billing/src/usage.ts index bc6c7063..994b3289 100644 --- a/services/billing/src/usage.ts +++ b/services/billing/src/usage.ts @@ -1,6 +1,5 @@ import { APIGatewayProxyResult, APIGatewayProxyEventBase } from 'aws-lambda'; -import { Context } from '@textile/context'; -import { Users } from '@textile/users'; +import { getAggregatedUsage } from './utils/usage'; if (!process?.env?.ENV) { throw new Error('ENV variable not set'); @@ -14,50 +13,29 @@ export interface AuthContext { pubkey: string; } -const { TestIdentity } = require('./id'); - -(async () => { - // set users key - // users.context.withAPIKey(TestIdentity.public.toString()); - - console.log(users.context); - try { - console.log('usage', JSON.stringify(usage.customer)); - } catch (e) { - console.log(e); - } -})(); - -const ctx = new Context('http://52.11.247.86:3007'); - // eslint-disable-next-line export const handler = async ( event: APIGatewayProxyEventBase ): Promise => { - const { uuid, pubkey } = event.requestContext.authorizer; + const { pubkey } = event.requestContext.authorizer; - try { - const users = await Users.withKeyInfo( - { - key: 'bwmvr74ev3sqmnj5hchooa2zkzm', - secret: 'byiyrna6l3wnfdp4a7aaxsiz573jrtuvrz3qhj6a', - }, - ctx - ); + let storage = 0; + let bandwidth = 0; - const usage = await users.getUsage({ - dependentUserKey: TestIdentity.public.toString(), - }); + try { + const aggregatedUsage = await getAggregatedUsage(pubkey); + bandwidth = aggregatedUsage.network_egress || 0; + storage = aggregatedUsage.stored_data || 0; } catch (e) { - // zero usage + console.log('usage error', e); + // zero usage? } - console.log(uuid); return { statusCode: 200, body: JSON.stringify({ - storage: Math.ceil(Math.random() * 1000000000), - bandwidth: Math.ceil(Math.random() * 10000000), + storage, + bandwidth, }), }; }; diff --git a/services/billing/src/utils/usage.ts b/services/billing/src/utils/usage.ts new file mode 100644 index 00000000..a66e4a3e --- /dev/null +++ b/services/billing/src/utils/usage.ts @@ -0,0 +1,55 @@ +import { Usage, Users, GetUsageResponse } from '@textile/users'; +import _ from 'lodash'; +import multibase from 'multibase'; + +import { Context } from '@textile/context'; + +const hexToBase32 = (hex: string): string => + Buffer.from( + multibase.encode('base32', Buffer.from(`08011220${hex}`, 'hex')) + ).toString(); + +export const getUsage = async (pubkey: string): Promise => { + const ctx = new Context(process.env.TXL_HUB_URL); + + const users = await Users.withKeyInfo( + { + key: process.env.TXL_USER_KEY, + secret: process.env.TXL_USER_SECRET, + }, + ctx + ); + + const dependentUserKey = hexToBase32(pubkey); + + return users.getUsage({ + dependentUserKey, + }); +}; + +export const aggregateUsageMap = ( + usageMap: Array<[string, Usage]> +): Record => { + const aggregated = _.reduce( + usageMap, + (acc: Record, [str, usage]) => { + if (!acc[str]) { + acc[str] = 0; + } + + acc[str] = usage.total; + + return acc; + }, + {} + ); + + return aggregated; +}; + +export const getAggregatedUsage = async ( + pubkey: string +): Promise> => { + const usage = await getUsage(pubkey); + return aggregateUsageMap(usage.customer.dailyUsageMap); +};