import { Request, Response } from "express"; import CategoryService from "../services/category.service"; import { CategoryProviderType } from "../categories/categoryProvider"; import { BaseController } from "./BaseController"; import logger from "../utils/logger"; import cors from "cors"; import { RequestSource } from "../utils/RequestUtils"; export class BrandSafetyController extends BaseController { categoryService: CategoryService; constructor() { super(); this.router.use(cors()); this.use("/check-brand-safety", this.checkBrandSafetyHandler); this.categoryService = CategoryService.getInstance(); } async checkBrandSafetyHandler(req: Request, res: Response) { try { const body = JSON.parse(req.body); const output = await this.checkBrandSafety(body); res.status(200).json(output); } catch (err: any) { logger.error(err); res.status(500).send(); } } async checkBrandSafety(input: CheckBrandSafetyInput): Promise { const output: CheckBrandSafetyOutput = { url: input.url ? await this.processUrlOrAppList(input.url, input.categoryProvider, input.fromCache) : undefined, android: input.android ? await this.processUrlOrAppList(input.android, input.categoryProvider, input.fromCache, { environmentType: "in-app", deviceOS: "android", }) : undefined, ios: input.ios ? await this.processUrlOrAppList(input.ios, input.categoryProvider, input.fromCache, { environmentType: "in-app", deviceOS: "ios", }) : undefined, }; return output; } async processUrlOrAppList( urlsOrApps: string[], categoryProvider?: CategoryProviderType, fromCache?: boolean, requestSource?: RequestSource ): Promise | undefined> { if (urlsOrApps.length === 0) { return undefined; } const output: Record = {}; for (const urlOrApp of urlsOrApps) { const categoryValue = await this.categoryService.getBrandSafetyDebugPurpose( urlOrApp, categoryProvider, fromCache, requestSource ); output[urlOrApp] = categoryValue.toSegmentList(); } return output; } } type CheckBrandSafetyInput = { url?: string[]; categoryProvider?: CategoryProviderType; fromCache?: boolean; android?: string[]; ios?: string[]; }; type CheckBrandSafetyOutput = { url?: Record; android?: Record; ios?: Record; };