117 lines
3.9 KiB
JavaScript
117 lines
3.9 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getInfluencerStats = exports.getInfluencerById = exports.getInfluencers = void 0;
|
|
const supabase_1 = __importDefault(require("../utils/supabase"));
|
|
const getInfluencers = async (c) => {
|
|
try {
|
|
const { platform, limit = '10', offset = '0', min_followers, max_followers, sort_by = 'followers_count', sort_order = 'desc' } = c.req.query();
|
|
let query = supabase_1.default
|
|
.from('influencers')
|
|
.select(`
|
|
influencer_id,
|
|
name,
|
|
platform,
|
|
profile_url,
|
|
followers_count,
|
|
video_count,
|
|
platform_count,
|
|
created_at,
|
|
updated_at
|
|
`);
|
|
// Apply filters
|
|
if (platform) {
|
|
query = query.eq('platform', platform);
|
|
}
|
|
if (min_followers) {
|
|
query = query.gte('followers_count', Number(min_followers));
|
|
}
|
|
if (max_followers) {
|
|
query = query.lte('followers_count', Number(max_followers));
|
|
}
|
|
// Apply sorting
|
|
if (sort_by && ['followers_count', 'video_count', 'created_at'].includes(sort_by)) {
|
|
query = query.order(sort_by, { ascending: sort_order === 'asc' });
|
|
}
|
|
// Apply pagination
|
|
query = query.range(Number(offset), Number(offset) + Number(limit) - 1);
|
|
const { data: influencers, error, count } = await query;
|
|
if (error) {
|
|
return c.json({ error: error.message }, 500);
|
|
}
|
|
return c.json({
|
|
influencers,
|
|
count,
|
|
limit: Number(limit),
|
|
offset: Number(offset)
|
|
});
|
|
}
|
|
catch (error) {
|
|
return c.json({ error: 'Internal server error' }, 500);
|
|
}
|
|
};
|
|
exports.getInfluencers = getInfluencers;
|
|
const getInfluencerById = async (c) => {
|
|
try {
|
|
const { influencer_id } = c.req.param();
|
|
const { data: influencer, error } = await supabase_1.default
|
|
.from('influencers')
|
|
.select(`
|
|
influencer_id,
|
|
name,
|
|
platform,
|
|
profile_url,
|
|
followers_count,
|
|
video_count,
|
|
platform_count,
|
|
created_at,
|
|
updated_at,
|
|
posts (
|
|
post_id,
|
|
title,
|
|
description,
|
|
published_at
|
|
)
|
|
`)
|
|
.eq('influencer_id', influencer_id)
|
|
.single();
|
|
if (error) {
|
|
return c.json({ error: 'Influencer not found' }, 404);
|
|
}
|
|
return c.json(influencer);
|
|
}
|
|
catch (error) {
|
|
return c.json({ error: 'Internal server error' }, 500);
|
|
}
|
|
};
|
|
exports.getInfluencerById = getInfluencerById;
|
|
const getInfluencerStats = async (c) => {
|
|
try {
|
|
const { platform } = c.req.query();
|
|
let query = supabase_1.default
|
|
.from('influencers')
|
|
.select('platform, followers_count, video_count');
|
|
if (platform) {
|
|
query = query.eq('platform', platform);
|
|
}
|
|
const { data: stats, error } = await query;
|
|
if (error) {
|
|
return c.json({ error: error.message }, 500);
|
|
}
|
|
const aggregatedStats = {
|
|
total_influencers: stats.length,
|
|
total_followers: stats.reduce((sum, item) => sum + (item.followers_count || 0), 0),
|
|
total_videos: stats.reduce((sum, item) => sum + (item.video_count || 0), 0),
|
|
average_followers: Math.round(stats.reduce((sum, item) => sum + (item.followers_count || 0), 0) / (stats.length || 1)),
|
|
average_videos: Math.round(stats.reduce((sum, item) => sum + (item.video_count || 0), 0) / (stats.length || 1))
|
|
};
|
|
return c.json(aggregatedStats);
|
|
}
|
|
catch (error) {
|
|
return c.json({ error: 'Internal server error' }, 500);
|
|
}
|
|
};
|
|
exports.getInfluencerStats = getInfluencerStats;
|