serpbear/pages/api/searchconsole.ts
towfiqi b2e97b2ebe feat: Adds the Ability to set Search Console Property type via Domain Settings.
- Previously only domain properties worked with SerpBear. This feature adds the ability to add URL properties as well.
- Adds a new field "search_console" in Domain Table.
- Adds a new Search Console option in Domain Settings Modal UI.
- When the  new "This is a URL Property" option is enabled, the exact Property URL should be provided.

closes #50
2024-02-06 23:42:28 +06:00

69 lines
2.9 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from 'next';
import db from '../../database/database';
import Domain from '../../database/models/domain';
import { fetchDomainSCData, readLocalSCData } from '../../utils/searchConsole';
import verifyUser from '../../utils/verifyUser';
type searchConsoleRes = {
data: SCDomainDataType|null
error?: string|null,
}
type searchConsoleCRONRes = {
status: string,
error?: string|null,
}
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
await db.sync();
const authorized = verifyUser(req, res);
if (authorized !== 'authorized') {
return res.status(401).json({ error: authorized });
}
if (req.method === 'GET') {
return getDomainSearchConsoleData(req, res);
}
if (req.method === 'POST') {
return cronRefreshSearchConsoleData(req, res);
}
return res.status(502).json({ error: 'Unrecognized Route.' });
}
const getDomainSearchConsoleData = async (req: NextApiRequest, res: NextApiResponse<searchConsoleRes>) => {
if (!req.query.domain && typeof req.query.domain !== 'string') return res.status(400).json({ data: null, error: 'Domain is Missing.' });
if (!!(process.env.SEARCH_CONSOLE_PRIVATE_KEY && process.env.SEARCH_CONSOLE_CLIENT_EMAIL) === false) {
return res.status(200).json({ data: null, error: 'Google Search Console Not Integrated' });
}
const domainname = (req.query.domain as string).replaceAll('-', '.').replaceAll('_', '-');
const localSCData = await readLocalSCData(domainname);
console.log(localSCData && localSCData.thirtyDays && localSCData.thirtyDays.length);
if (localSCData && localSCData.thirtyDays && localSCData.thirtyDays.length) {
return res.status(200).json({ data: localSCData });
}
try {
const query = { domain: domainname };
const foundDomain:Domain| null = await Domain.findOne({ where: query });
const domainObj: DomainType = foundDomain && foundDomain.get({ plain: true });
const scData = await fetchDomainSCData(domainObj);
return res.status(200).json({ data: scData });
} catch (error) {
console.log('[ERROR] Getting Search Console Data for: ', domainname, error);
return res.status(400).json({ data: null, error: 'Error Fetching Data from Google Search Console.' });
}
};
const cronRefreshSearchConsoleData = async (req: NextApiRequest, res: NextApiResponse<searchConsoleCRONRes>) => {
try {
const allDomainsRaw = await Domain.findAll();
const Domains: DomainType[] = allDomainsRaw.map((el) => el.get({ plain: true }));
for (const domain of Domains) {
await fetchDomainSCData(domain);
}
return res.status(200).json({ status: 'completed' });
} catch (error) {
console.log('[ERROR] CRON Updating Search Console Data. ', error);
return res.status(400).json({ status: 'failed', error: 'Error Fetching Data from Google Search Console.' });
}
};