Skip to content

Commit 07fb90c

Browse files
Clean up code to make documenting easier
1 parent b827d07 commit 07fb90c

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import Stripe from "https://esm.sh/stripe@10.13.0?target=deno&deno-std=0.165.0"
2+
import * as OneSignal from "https://esm.sh/@onesignal/node-onesignal@1.0.
23

4+
// OneSignal
5+
export const _OnesignalAppId_ = Deno.env.get("ONESIGNAL_APP_ID")!
6+
const _OnesignalUserAuthKey_ = Deno.env.get("USER_AUTH_KEY")!
7+
export const _OnesignalRestApiKey_ = Deno.env.get("ONESIGNAL_REST_API_KEY")!
8+
9+
const configuration = OneSignal.createConfiguration({
10+
userKey: _OnesignalUserAuthKey_,
11+
appKey: _OnesignalAppId_,
12+
})
13+
export const onesignal = new OneSignal.DefaultApi(configuration)
14+
15+
// Supabase
316
export const _SupabaseUrl_ = Deno.env.get("SUPABASE_URL")!
417
export const _SupabaseServiceRoleKey_ = Deno.env.get(
518
"SUPABASE_SERVICE_ROLE_KEY"
619
)!
20+
21+
// Stripe
722
export const _StripePublishableKey_ = Deno.env.get("STRIPE_PUBLISHABLE_KEY")!
823
export const _StripeWebhookSecret_ = Deno.env.get("STRIPE_WEBHOOK_SECRET")!
9-
1024
const _stripeSecretKey_ = Deno.env.get("STRIPE_SECRET_KEY")!
1125
export const stripe = Stripe(_stripeSecretKey_, {
1226
httpClient: Stripe.createFetchHttpClient(),
1327
})
14-
export const _OnesignalAppId_ = Deno.env.get("ONESIGNAL_APP_ID")!
15-
export const _OnesignalUserAuthKey_ = Deno.env.get("USER_AUTH_KEY")!
16-
export const _OnesignalRestApiKey_ = Deno.env.get("ONESIGNAL_REST_API_KEY")!
1728

1829
// For debugging environment
1930
// console.log("Runtime environmentDeno\n\t", Deno.env.toObject())

supabase/functions/push-order-confirmation-to-customer/index.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import { serve } from "https://deno.land/std@0.131.0/http/server.ts"
22
import * as OneSignal from "https://esm.sh/@onesignal/node-onesignal@1.0.0-beta7"
33
import {
4+
onesignal,
45
_OnesignalAppId_,
56
_OnesignalRestApiKey_,
67
_OnesignalUserAuthKey_,
78
} from "../_utils/config.ts"
89
import { getCustomerProfile } from "../_utils/supabase.ts"
910

10-
const notifMessage = (amount: number, currency: string) =>
11+
const generateMessage = (amount: number, currency: string) =>
1112
`You just spent ${amount / 100} ${(currency as String).toUpperCase()}.`
1213

1314
serve(async (req) => {
14-
// Load secrets
15-
16-
// Create OneSignal client
17-
const configuration = OneSignal.createConfiguration({
18-
userKey: _OnesignalUserAuthKey_,
19-
appKey: _OnesignalRestApiKey_,
20-
})
21-
const onesignalClient = new OneSignal.DefaultApi(configuration)
22-
2315
try {
24-
const json = await req.json()
25-
const { record } = json
16+
const { record } = await req.json()
2617
const customerId = record.stripe_customer_id
2718
const profile: string | null = await getCustomerProfile(customerId)
2819
if (!profile) {
@@ -33,10 +24,10 @@ serve(async (req) => {
3324
const notification = new OneSignal.Notification()
3425
notification.app_id = _OnesignalAppId_
3526
notification.include_external_user_ids = [profile]
36-
notification.contents = { en: notifMessage(record.amount, record.currency) }
37-
const onesignalApiRes = await onesignalClient.createNotification(
38-
notification
39-
)
27+
notification.contents = {
28+
en: generateMessage(record.amount, record.currency),
29+
}
30+
const onesignalApiRes = await onesignal.createNotification(notification)
4031

4132
return new Response(
4233
JSON.stringify({ onesignalResponse: onesignalApiRes }),

supabase/functions/push-to-all-subs/index.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
import { serve } from "https://deno.land/std@0.131.0/http/server.ts"
22
import * as OneSignal from "https://esm.sh/@onesignal/node-onesignal@1.0.0-beta7"
3+
import {
4+
onesignal,
5+
_OnesignalAppId_,
6+
_OnesignalUserAuthKey_,
7+
} from "../_utils/config.ts"
38

49
serve(async (req) => {
5-
// Load secrets
6-
const appId = Deno.env.get("ONESIGNAL_APP_ID")!
7-
const userAuthKey = Deno.env.get("USER_AUTH_KEY")!
8-
const restApiKey = Deno.env.get("ONESIGNAL_REST_API_KEY")!
9-
1010
// Create OneSignal client
11-
const configuration = OneSignal.createConfiguration({
12-
userKey: userAuthKey,
13-
appKey: restApiKey,
14-
})
15-
const client = new OneSignal.DefaultApi(configuration)
1611

1712
try {
18-
const data = await req.json()
19-
const { message } = data
13+
const { message } = await req.json()
2014

2115
// Build OneSignal notification object
2216
const notification = new OneSignal.Notification()
23-
notification.app_id = appId
17+
notification.app_id = _OnesignalAppId_
2418
notification.contents = {
2519
en: message,
2620
}
2721
notification.included_segments = ["Subscribed Users"]
2822

2923
// // Call OneSignal API to push notification
30-
const res = await client.createNotification(notification)
24+
const res = await onesignal.createNotification(notification)
3125
console.log("OneSignal response", res)
3226

3327
return new Response(JSON.stringify(data), {

0 commit comments

Comments
 (0)