Setup Trigger to fetch verified accounts from 3box when ETH address gets updated or user gets inserted

This commit is contained in:
Hammad Jutt
2020-08-21 11:34:22 -06:00
parent caca26b1ce
commit 13ab1eef2f
17 changed files with 255 additions and 74 deletions

View File

@@ -49,7 +49,7 @@ export const migrateSourceCredAccounts = async (
).json();
const accountOnConflict = {
constraint: Account_Constraint.AccountIdentifierTypePlayerKey,
constraint: Account_Constraint.AccountIdentifierTypeKey,
update_columns: [Account_Update_Column.Identifier],
};
@@ -91,6 +91,5 @@ export const migrateSourceCredAccounts = async (
],
},
});
res.json(result);
};

View File

@@ -1,7 +1,6 @@
import Box, { VerifiedAccounts } from '3box';
import { Request, Response } from 'express';
import { client } from '../../../lib/hasuraClient';
import { updateVerifiedAccounts } from './updateVerifiedAccounts';
export const updateBoxProfileHandler = async (
req: Request,
@@ -15,61 +14,7 @@ export const updateBoxProfileHandler = async (
throw new Error('expected role player');
}
const data = await client.GetPlayer({ playerId });
const ethAddress = data.Player_by_pk?.ethereum_address;
if (!ethAddress) {
throw new Error('unknown-player');
}
const boxProfile = await Box.getProfile(ethAddress);
const verifiedProfile = await Box.getVerifiedAccounts(boxProfile);
const result = await updateVerifiedProfiles(playerId, verifiedProfile);
const result = await updateVerifiedAccounts(playerId);
res.json(result);
};
async function updateVerifiedProfiles(
playerId: string,
verifiedProfiles: VerifiedAccounts,
): Promise<UpdateBoxProfileResponse> {
const updatedProfiles: string[] = [];
if (verifiedProfiles.github) {
const result = await client.UpsertAccount({
objects: [
{
player_id: playerId,
type: 'GITHUB',
identifier: verifiedProfiles.github.username,
},
],
});
if (result.insert_Account?.affected_rows === 0) {
throw new Error('Error while upserting github profile');
}
updatedProfiles.push('github');
}
if (verifiedProfiles.twitter) {
const result = await client.UpsertAccount({
objects: [
{
player_id: playerId,
type: 'TWITTER',
identifier: verifiedProfiles.twitter.username,
},
],
});
if (result.insert_Account?.affected_rows === 0) {
throw new Error('Error while upserting github profile');
}
updatedProfiles.push('twitter');
}
return {
success: true,
updatedProfiles,
};
}

View File

@@ -0,0 +1,56 @@
import Box from '3box';
import { client } from '../../../lib/hasuraClient';
export async function updateVerifiedAccounts(
playerId: string,
): Promise<UpdateBoxProfileResponse> {
const updatedProfiles: string[] = [];
const data = await client.GetPlayer({ playerId });
const ethAddress = data.Player_by_pk?.ethereum_address;
if (!ethAddress) {
throw new Error('unknown-player');
}
const boxProfile = await Box.getProfile(ethAddress);
const verifiedAccounts = await Box.getVerifiedAccounts(boxProfile);
if (verifiedAccounts.github) {
const result = await client.UpsertAccount({
objects: [
{
player_id: playerId,
type: 'GITHUB',
identifier: verifiedAccounts.github.username,
},
],
});
if (result.insert_Account?.affected_rows === 0) {
throw new Error('Error while upserting github profile');
}
updatedProfiles.push('github');
}
if (verifiedAccounts.twitter) {
const result = await client.UpsertAccount({
objects: [
{
player_id: playerId,
type: 'TWITTER',
identifier: verifiedAccounts.twitter.username,
},
],
});
if (result.insert_Account?.affected_rows === 0) {
throw new Error('Error while upserting github profile');
}
updatedProfiles.push('twitter');
}
return {
success: true,
updatedProfiles,
};
}

View File

@@ -20,7 +20,7 @@ export const UpsertAccount = gql`
insert_Account(
objects: $objects
on_conflict: {
constraint: Account_identifier_type_player_key
constraint: Account_identifier_type_key
update_columns: [identifier]
}
) {

View File

@@ -4,6 +4,7 @@ import { asyncHandlerWrapper } from '../lib/apiHelpers';
import { actionRoutes } from './actions/routes';
import { authHandler } from './auth-webhook/handler';
import { remoteSchemaRoutes } from './remote-schemas/routes';
import { triggerHandler } from './triggers/handler';
export const router = express.Router();
@@ -16,6 +17,7 @@ router.get('/healthz', (_, res) => {
});
router.get('/auth-webhook', asyncHandlerWrapper(authHandler));
router.post('/triggers', asyncHandlerWrapper(triggerHandler));
router.use('/actions', actionRoutes);

View File

@@ -0,0 +1,17 @@
import { Player } from '../../lib/autogen/hasura-sdk';
import { updateVerifiedAccounts } from '../actions/updateBoxProfile/updateVerifiedAccounts';
import { TriggerPayload } from './types';
export const fetchBoxVerifiedAccounts = async (
payload: TriggerPayload<Player>,
) => {
const address = payload.event.data.new?.ethereum_address;
if (!address) {
return;
}
const playerId = payload.event.data.new?.id;
await updateVerifiedAccounts(playerId);
};

View File

@@ -0,0 +1,29 @@
import { Request, Response } from 'express';
import { ParamsDictionary } from 'express-serve-static-core';
import { fetchBoxVerifiedAccounts } from './fetchBoxVerifiedAccounts';
import { TriggerPayload } from './types';
const TRIGGERS = {
fetchBoxVerifiedAccounts,
};
export const triggerHandler = async (
req: Request<ParamsDictionary, never, TriggerPayload>,
res: Response,
): Promise<void> => {
const role = req.body.event?.session_variables?.['x-hasura-role'];
if (role !== 'admin') {
throw new Error('Unauthorized');
}
const trigger = TRIGGERS[req.body.trigger.name as keyof typeof TRIGGERS];
if (trigger) {
await trigger(req.body);
res.sendStatus(200);
} else {
res.sendStatus(404);
}
};

View File

@@ -0,0 +1,23 @@
export interface TriggerPayload<T = any> {
event: {
session_variables: { [x: string]: string };
op: 'INSERT' | 'UPDATE' | 'DELETE' | 'MANUAL';
data: {
old: T | null;
new: T | null;
};
};
created_at: string;
id: string;
delivery_info: {
max_retries: number;
current_retry: number;
};
trigger: {
name: string;
};
table: {
schema: string;
name: string;
};
}