shop-service.js 1.26 KB
const _ = require('lodash');
const ShopModel = require('./shop-api');

const {CHANNELS} = require('./vars');

const BRAND_TYPE = {
    shop: 2
};

function getShopUrl(domain, shopId) {
    return `/shop/${domain}-${shopId}.html`;
}

function indexUrl(type, page) {
    return `/sitemap_${type}/shop_${page}.xml`;
}

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
        this.shopModel = new ShopModel(ctx);
    }

    async _getShopUrl() {
        let shopUrls = [];

        await Promise.map(Object.values(CHANNELS), async (channel) => {
            let {data: all_list} = await this.shopModel.getBrandListData(channel);

            _.forEach(all_list, shops => {
                _.forEach(shops, shop => {
                    if (+shop.type === BRAND_TYPE.shop) {
                        let shopId = shop.shop_id;
                        let domain = shop.brand_domain || 'default_domain';

                        shopUrls.push(getShopUrl(domain.toLowerCase(), shopId));
                    }
                });
            });
        }, {concurrency: 1});

        return shopUrls;
    }

    async getUrls() {
        return this._getShopUrl();
    }

    async getIndex(type) {
        return [indexUrl(type, 1)];
    }
};