Update dependencies and fix updateBoxProfile handler.ts

This commit is contained in:
Hammad Jutt
2020-08-05 01:30:15 -06:00
parent c82ba547b6
commit dbe4f046d8
8 changed files with 3967 additions and 7719 deletions

View File

@@ -1,37 +1,36 @@
import Box, { VerifiedAccounts } from '3box';
import { Request, Response } from 'express';
import { gql } from 'graphql-request';
import { hasuraQuery } from '../../../lib/hasuraHelpers';
import { getPlayerETHAddress } from '../../../lib/playerHelpers';
const getPlayerQuery = `
query GetPlayer ($playerId: uuid!) {
Player(
where: {
id: { _eq: $playerId }
}
) {
id
Accounts {
identifier
type
type PlayerResult = {
Player: Array<{
id: string;
ethereum_address: string | null | undefined;
}>;
};
const getPlayerQuery = gql`
query GetPlayer($playerId: uuid!) {
Player(where: { id: { _eq: $playerId } }) {
id
ethereum_address
}
}
}
`;
const upsertAccount = `
mutation upsert_Account($objects: [Account_insert_input!]!) {
insert_Account (
objects: $objects,
on_conflict: {
constraint: Account_identifier_type_player_key,
update_columns: [identifier]
const upsertAccount = gql`
mutation upsert_Account($objects: [Account_insert_input!]!) {
insert_Account(
objects: $objects
on_conflict: {
constraint: Account_identifier_type_player_key
update_columns: [identifier]
}
) {
affected_rows
}
) {
affected_rows
}
}
`;
export const updateBoxProfileHandler = async (
@@ -46,16 +45,16 @@ export const updateBoxProfileHandler = async (
throw new Error('expected role player');
}
const data = await hasuraQuery(getPlayerQuery, {
const data = await hasuraQuery<PlayerResult>(getPlayerQuery, {
playerId,
});
const player = data.Player[0];
if (!player) {
const ethAddress = data.Player[0]?.ethereum_address;
if (!ethAddress) {
throw new Error('unknown-player');
}
const ethAddress = getPlayerETHAddress(player);
const boxProfile = await Box.getProfile(ethAddress);
const verifiedProfile = await Box.getVerifiedAccounts(boxProfile);
const result = await updateVerifiedProfiles(playerId, verifiedProfile);

View File

@@ -1,21 +1,22 @@
import fetch from 'node-fetch';
import { GraphQLClient } from 'graphql-request';
import { Variables } from 'graphql-request/dist/types';
import { CONFIG } from '../config';
export async function hasuraQuery(query: string, qv: any = {}) {
const result = await fetch(CONFIG.graphqlURL, {
method: 'POST',
body: JSON.stringify({ query, variables: qv }),
export async function hasuraQuery<T = any>(
query: string,
variables?: Variables,
): Promise<T> {
const graphQLClient = new GraphQLClient(CONFIG.graphqlURL, {
headers: {
'Content-Type': 'application/json',
'x-hasura-access-key': CONFIG.adminKey,
},
});
const { errors, data } = await result.json();
if (errors) {
throw new Error(JSON.stringify(errors));
try {
return await graphQLClient.request<T>(query, variables);
} catch (error) {
throw new Error(JSON.stringify(error, undefined, 2));
}
return data;
}

View File

@@ -1,3 +0,0 @@
export function getPlayerETHAddress(player: any) {
return player.Accounts.find((a: any) => a.type === 'ETHEREUM').identifier;
}