Untitled

 avatar
unknown
plain_text
4 months ago
1.5 kB
8
Indexable
app.get('/api/carousel/home/:idCarousel/:language', app.wrap(async (req, res) => {
    const { idCarousel, language } = req.params;
    // LANGUAGE
    const lang = language == 'en' ? 'en' : 'vi'; //tạm có 2 cái => set cứng chỗ này => đổi lại khúc sau

    const carousel = await app.model.fwCarousel.get({ id: Number(idCarousel) });
    if (!carousel) {
        return;
    }

    const items = await app.model.fwCarouselItem.getAll({ carouselId: Number(idCarousel), active: true }, undefined, [['priority', 'ASC']]);

    const parseText = (jsonObj: object | string | undefined): string => {
        if (!jsonObj) return '';
        if (typeof jsonObj == 'string') {
            try {
                const parsed = JSON.parse(jsonObj) as Record<string, string>;
                return parsed[lang] || parsed['vi'] || '';
            } catch {
                return jsonObj;
            }
        }
        const obj = jsonObj as Record<string, string>;
        return obj[lang] || obj['vi'] || '';
    };

    const parsedItems = (items || []).map((item: IFwCarouselItemAttributes) => ({
        id: item.id,
        title: parseText(item.title),
        subtitle: parseText(item.subtitle),
        image: item.image || null,
        link: item.link,
        type: item.type || 'image'
    }));

    res.send({ id: carousel.id, title: parseText(carousel.title), height: carousel.height, single: carousel.single ?? false, items: parsedItems });
}));

Editor is loading...
Leave a Comment