Skip to content

Commit

Permalink
fix(migration): use api only if needed for updateRewardsToIds
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed May 9, 2022
1 parent 88d5c31 commit d000925
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ApiClient } from '@twurple/api';
import { StaticAuthProvider } from '@twurple/auth';
import { MigrationInterface, QueryRunner } from 'typeorm';

let rewardsList: null | any[] = null;

export class updateRewardsToIds1651671003485 implements MigrationInterface {
name = 'updateRewardsToIds1651671003485';

Expand All @@ -17,17 +19,17 @@ export class updateRewardsToIds1651671003485 implements MigrationInterface {
})?.value;

if (!accessToken || !clientId || !broadcasterId) {
console.log('Missing broadcaster access token or client ID or broadcaster ID');
return;
}

try {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
const rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));

const rewardAlerts = await queryRunner.query(`SELECT * from \`alert_reward_redeem\``);
for (const alert of rewardAlerts) {
if (!rewardsList) {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));
}
const reward = rewardsList.find(o => o.title === alert.rewardId);
if (reward) {
console.log(`Changing reward name ${alert.rewardId} to ${reward.id}`);
Expand All @@ -38,6 +40,12 @@ export class updateRewardsToIds1651671003485 implements MigrationInterface {
const events = await queryRunner.query(`SELECT * from \`event\``);
for (const event of events) {
if (event.name === 'reward-redeemed') {
if (!rewardsList) {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));
}

let definitions = JSON.parse<any>(event.definitions);
const reward = rewardsList.find(o => o.title === definitions.titleOfReward);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ApiClient } from '@twurple/api';
import { StaticAuthProvider } from '@twurple/auth';
import { MigrationInterface, QueryRunner } from 'typeorm';

let rewardsList: null | any[] = null;

export class updateRewardsToIds1651671003485 implements MigrationInterface {
name = 'updateRewardsToIds1651671003485';

Expand All @@ -17,17 +19,17 @@ export class updateRewardsToIds1651671003485 implements MigrationInterface {
})?.value;

if (!accessToken || !clientId || !broadcasterId) {
console.log('Missing broadcaster access token or client ID or broadcaster ID');
return;
}

try {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
const rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));

const rewardAlerts = await queryRunner.query(`SELECT * from "alert_reward_redeem"`);
for (const alert of rewardAlerts) {
if (!rewardsList) {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));
}
const reward = rewardsList.find(o => o.title === alert.rewardId);
if (reward) {
console.log(`Changing reward name ${alert.rewardId} to ${reward.id}`);
Expand All @@ -38,6 +40,11 @@ export class updateRewardsToIds1651671003485 implements MigrationInterface {
const events = await queryRunner.query(`SELECT * from "event"`);
for (const event of events) {
if (event.name === 'reward-redeemed') {
if (!rewardsList) {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));
}
let definitions = JSON.parse<any>(event.definitions);
const reward = rewardsList.find(o => o.title === definitions.titleOfReward);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ApiClient } from '@twurple/api';
import { StaticAuthProvider } from '@twurple/auth';
import { MigrationInterface, QueryRunner } from 'typeorm';

let rewardsList: null | any[] = null;

export class updateRewardsToIds1651671003485 implements MigrationInterface {
name = 'updateRewardsToIds1651671003485';

Expand All @@ -17,17 +19,17 @@ export class updateRewardsToIds1651671003485 implements MigrationInterface {
})?.value;

if (!accessToken || !clientId || !broadcasterId) {
console.log('Missing broadcaster access token or client ID or broadcaster ID');
return;
}

try {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
const rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));

const rewardAlerts = await queryRunner.query(`SELECT * from "alert_reward_redeem"`);
for (const alert of rewardAlerts) {
if (!rewardsList) {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));
}
const reward = rewardsList.find(o => o.title === alert.rewardId);
if (reward) {
console.log(`Changing reward name ${alert.rewardId} to ${reward.id}`);
Expand All @@ -38,6 +40,11 @@ export class updateRewardsToIds1651671003485 implements MigrationInterface {
const events = await queryRunner.query(`SELECT * from "event"`);
for (const event of events) {
if (event.name === 'reward-redeemed') {
if (!rewardsList) {
const authProvider = new StaticAuthProvider(JSON.parse<string>(clientId), JSON.parse<string>(accessToken));
const client = new ApiClient({ authProvider });
rewardsList = await client.channelPoints.getCustomRewards(JSON.parse<string>(broadcasterId));
}
let definitions = JSON.parse<any>(event.definitions);
const reward = rewardsList.find(o => o.title === definitions.titleOfReward);

Expand Down

0 comments on commit d000925

Please sign in to comment.