This commit is contained in:
Hongbo Wu
2022-11-21 17:12:18 +08:00
parent 9af670a5bc
commit 06a3f906f8
3 changed files with 11 additions and 13 deletions

View File

@ -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')

View File

@ -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,

View File

@ -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<UserDeviceToken[]> => {
): Promise<DeviceToken[]> => {
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 = (