export const updatePlatformStats = async (_args, context) => {
const unverifiedUsers = await context.entities.UnverifiedUser.findMany({
where: {
platformUsername: {
not: null,
},
},
});
for (let user of unverifiedUsers) {
const fullResponse = await fetch(
`https://api.someapi.com/v1/profile-videos?handle=${user.platformUsername}&amount=10
);
if (!fullResponse.ok) {
continue;
}
const videos = await fullResponse.json();
const formattedVideos = Array.from(videos).map((video: any) => {
return {
hashtags: video.challenges.map((challenge) => challenge.title),
stats: video.stats || null,
description: video.desc,
createdAt: video.createTime || null,
};
});
try {
const res = await context.entities.UnverifiedUserStats.upsert({
where: {
userId: user.id,
},
update: {
totalEngagementRate: parsedEngagementRate,
averageViews,
labelsUsed: [],
hashtagsUsed,
lastVideoDate,
},
create: {
totalEngagementRate: parsedEngagementRate,
averageViews,
labelsUsed: [],
hashtagsUsed,
lastVideoDate,
user: { connect: { id: user.id } },
},
});
} catch (error) {
console.error('Database upsert failed:', error);
}
}
};
export const updatePlatformStats = async (_args, context) => {
const unverifiedUsers = await context.entities.UnverifiedUser.findMany({
where: {
platformUsername: {
not: null,
},
},
});
for (let user of unverifiedUsers) {
const fullResponse = await fetch(
`https://api.someapi.com/v1/profile-videos?handle=${user.platformUsername}&amount=10
);
if (!fullResponse.ok) {
continue;
}
const videos = await fullResponse.json();
const formattedVideos = Array.from(videos).map((video: any) => {
return {
hashtags: video.challenges.map((challenge) => challenge.title),
stats: video.stats || null,
description: video.desc,
createdAt: video.createTime || null,
};
});
try {
const res = await context.entities.UnverifiedUserStats.upsert({
where: {
userId: user.id,
},
update: {
totalEngagementRate: parsedEngagementRate,
averageViews,
labelsUsed: [],
hashtagsUsed,
lastVideoDate,
},
create: {
totalEngagementRate: parsedEngagementRate,
averageViews,
labelsUsed: [],
hashtagsUsed,
lastVideoDate,
user: { connect: { id: user.id } },
},
});
} catch (error) {
console.error('Database upsert failed:', error);
}
}
};