From 06a3f906f8467db1fe5c0e353a0c7231b8390eed Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 21 Nov 2022 17:12:18 +0800 Subject: [PATCH] Fix bug --- packages/rule-handler/src/index.ts | 2 +- packages/rule-handler/src/rule.ts | 4 ++-- packages/rule-handler/src/sendNotification.ts | 18 ++++++++---------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/rule-handler/src/index.ts b/packages/rule-handler/src/index.ts index 0087bada9..fc5e5533f 100644 --- a/packages/rule-handler/src/index.ts +++ b/packages/rule-handler/src/index.ts @@ -70,7 +70,7 @@ export const getAuthToken = async ( export const ruleHandler = Sentry.GCPFunction.wrapHttpFunction( async (req: Request, res: Response) => { - const apiEndpoint = process.env.API_ENDPOINT + const apiEndpoint = process.env.REST_BACKEND_ENDPOINT const jwtSecret = process.env.JWT_SECRET if (!apiEndpoint || !jwtSecret) { throw new Error('REST_BACKEND_ENDPOINT or JWT_SECRET not set') diff --git a/packages/rule-handler/src/rule.ts b/packages/rule-handler/src/rule.ts index 3a1522e0a..7343fcdfc 100644 --- a/packages/rule-handler/src/rule.ts +++ b/packages/rule-handler/src/rule.ts @@ -1,6 +1,6 @@ import { getBatchMessages, - getUserDeviceTokens, + getDeviceTokens, sendBatchPushNotifications, } from './sendNotification' import { getAuthToken, PubSubData } from './index' @@ -107,7 +107,7 @@ export const sendNotification = async ( jwtSecret: string ) => { // get device tokens by calling api - const tokens = await getUserDeviceTokens(userId, apiEndpoint, jwtSecret) + const tokens = await getDeviceTokens(userId, apiEndpoint, jwtSecret) const batchMessages = getBatchMessages( messages, diff --git a/packages/rule-handler/src/sendNotification.ts b/packages/rule-handler/src/sendNotification.ts index 775da6d71..684f858e6 100644 --- a/packages/rule-handler/src/sendNotification.ts +++ b/packages/rule-handler/src/sendNotification.ts @@ -8,7 +8,7 @@ import { import axios from 'axios' import { getAuthToken } from './index' -export interface UserDeviceToken { +export interface DeviceToken { id: string token: string userId: string @@ -20,24 +20,23 @@ initializeApp({ credential: applicationDefault(), }) -export const getUserDeviceTokens = async ( +export const getDeviceTokens = async ( userId: string, apiEndpoint: string, jwtSecret: string -): Promise => { +): Promise => { const auth = await getAuthToken(userId, jwtSecret) const data = JSON.stringify({ query: `query { - userDeviceTokens(userId: "${userId}") { - ... on UserDeviceTokensError { + deviceTokens { + ... on DeviceTokensError { errorCodes } - ... on UserDeviceTokensSuccess { - userDeviceTokens { + ... on DeviceTokensSuccess { + deviceTokens { id token - userId createdAt } } @@ -53,8 +52,7 @@ export const getUserDeviceTokens = async ( }) // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - return response.data.data.userDeviceTokens - .userDeviceTokens as UserDeviceToken[] + return response.data.data.deviceTokens.deviceTokens as DeviceToken[] } export const getBatchMessages = (