Skip to content

Commit

Permalink
fix: fixes local SC data not being removed on deleting domain.
Browse files Browse the repository at this point in the history
  • Loading branch information
towfiqi committed Jan 15, 2024
1 parent faa88c9 commit cca9f95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 6 additions & 6 deletions pages/api/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Domain from '../../database/models/domain';
import Keyword from '../../database/models/keyword';
import getdomainStats from '../../utils/domains';
import verifyUser from '../../utils/verifyUser';
import { removeLocalSCData } from '../../utils/searchConsole';

type DomainsGetRes = {
domains: DomainType[]
Expand All @@ -18,6 +19,7 @@ type DomainsAddResponse = {
type DomainsDeleteRes = {
domainRemoved: number,
keywordsRemoved: number,
SCDataRemoved: boolean,
error?: string|null,
}

Expand Down Expand Up @@ -87,19 +89,17 @@ const addDomain = async (req: NextApiRequest, res: NextApiResponse<DomainsAddRes

export const deleteDomain = async (req: NextApiRequest, res: NextApiResponse<DomainsDeleteRes>) => {
if (!req.query.domain && typeof req.query.domain !== 'string') {
return res.status(400).json({ domainRemoved: 0, keywordsRemoved: 0, error: 'Domain is Required!' });
return res.status(400).json({ domainRemoved: 0, keywordsRemoved: 0, SCDataRemoved: false, error: 'Domain is Required!' });
}
try {
const { domain } = req.query || {};
const removedDomCount: number = await Domain.destroy({ where: { domain } });
const removedKeywordCount: number = await Keyword.destroy({ where: { domain } });
return res.status(200).json({
domainRemoved: removedDomCount,
keywordsRemoved: removedKeywordCount,
});
const SCDataRemoved = await removeLocalSCData(domain as string);
return res.status(200).json({ domainRemoved: removedDomCount, keywordsRemoved: removedKeywordCount, SCDataRemoved });
} catch (error) {
console.log('[ERROR] Deleting Domain: ', req.query.domain, error);
return res.status(400).json({ domainRemoved: 0, keywordsRemoved: 0, error: 'Error Deleting Domain' });
return res.status(400).json({ domainRemoved: 0, keywordsRemoved: 0, SCDataRemoved: false, error: 'Error Deleting Domain' });
}
};

Expand Down
17 changes: 16 additions & 1 deletion utils/searchConsole.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { auth, searchconsole_v1 } from '@googleapis/searchconsole';
import { readFile, writeFile } from 'fs/promises';
import { readFile, writeFile, unlink } from 'fs/promises';
import { getCountryCodeFromAlphaThree } from './countries';

export type SCDomainFetchError = {
Expand Down Expand Up @@ -189,4 +189,19 @@ export const updateLocalSCData = async (domain:string, scDomainData?:SCDomainDat
return scDomainData || emptyData;
};

/**
* The function removes the domain-specific Seach Console data stored in a local JSON file.
* @param {string} domain - The `domain` parameter is a string that represents the domain for which the SC data file will be removed.
* @returns {Promise<boolean>} - Returns true if file was removed, else returns false.
*/
export const removeLocalSCData = async (domain:string): Promise<boolean> => {
const filePath = `${process.cwd()}/data/SC_${domain}.json`;
try {
await unlink(filePath);
return true;
} catch (error) {
return false;
}
};

export default fetchSearchConsoleData;

0 comments on commit cca9f95

Please sign in to comment.