38 lines
2.2 KiB
JavaScript
38 lines
2.2 KiB
JavaScript
import { pathToFileURL } from 'node:url';
|
|
import { productionSettings } from './production-settings.mjs';
|
|
|
|
export async function verifyAbc(env = process.env, fetchImpl = fetch) {
|
|
const { vars, secrets } = productionSettings(env);
|
|
const endpoint = vars.ABC_API_BASE_URL + '/api/projects/' + encodeURIComponent(vars.ABC_PROJECT_ID)
|
|
+ '/channels/' + encodeURIComponent(vars.ABC_CHANNEL_ID) + '/fields';
|
|
let response;
|
|
try {
|
|
response = await fetchImpl(endpoint, {
|
|
method: 'GET', redirect: 'error', signal: AbortSignal.timeout(15000),
|
|
headers: { accept: 'application/json', 'x-api-key': secrets.ABC_API_KEY }
|
|
});
|
|
} catch {
|
|
throw new Error('Production ABC API request failed; check PROD_ABC_API_BASE_URL, HTTPS and connectivity');
|
|
}
|
|
if (!response.ok) {
|
|
throw new Error('Production ABC API returned HTTP ' + response.status
|
|
+ '; check PROD_ABC_API_KEY, PROD_ABC_PROJECT_ID and PROD_ABC_CHANNEL_ID');
|
|
}
|
|
let body;
|
|
try { body = await response.json(); } catch { throw new Error('Production ABC fields response is not JSON'); }
|
|
const fields = Array.isArray(body) ? body : body?.items || body?.data?.items || body?.data;
|
|
if (!Array.isArray(fields)) throw new Error('Production ABC fields response is not a field list');
|
|
const keys = new Set(fields.map((field) => field.key));
|
|
const missing = ['title', 'contents', 'Category'].filter((key) => !keys.has(key));
|
|
if (missing.length) throw new Error('Production Q&A channel is missing required fields: ' + missing.join(', '));
|
|
const categories = new Set((fields.find((field) => field.key === 'Category').options || []).map((option) => option.key));
|
|
const missingCategories = ['ERROR_QNA', 'IMPROVEMENT_QNA', 'GENERAL_QNA'].filter((key) => !categories.has(key));
|
|
if (missingCategories.length) throw new Error('Production Q&A channel is missing category options: ' + missingCategories.join(', '));
|
|
console.log('Production ABC API verified: ' + vars.ABC_API_BASE_URL + ', project ' + vars.ABC_PROJECT_ID
|
|
+ ', channel ' + vars.ABC_CHANNEL_ID + '. No feedback data was changed.');
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
try { await verifyAbc(); } catch (error) { console.error(error.message); process.exitCode = 1; }
|
|
}
|