"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EndpointsController = void 0;
const util_method_1 = require("./../methods/util.method");
const abstract_controller_1 = require("./abstract.controller");
const endpoint_model_1 = require("../models/endpoint.model");
const endpoint_method_1 = require("../methods/endpoint.method");
class EndpointsController extends abstract_controller_1.AbstractController {
    constructor() {
        super(endpoint_model_1.EndpointsModel);
        this.methods = new endpoint_method_1.EndpointsMethods(endpoint_model_1.EndpointsModel);
        this.utilsMethods = new util_method_1.UtilsMethods(endpoint_model_1.EndpointsModel);
    }
    async createItem(req, res) {
        try {
            const endpoints = this.utilsMethods.endpointsUtils(req);
            let allEndpoints = endpoints.docs.reduce((prev, next) => {
                return [
                    ...prev,
                    ...next.methods.map((method) => {
                        return {
                            path: `${method}${next.path}`,
                            store_id: req.headers.store,
                        };
                    }),
                ];
            }, []);
            allEndpoints = allEndpoints.filter(async (endpoint) => {
                return await !this.model.findOne({
                    path: endpoint.path,
                });
            });
            const data = await this.model.bulkWrite(allEndpoints.map((endpoint) => ({
                updateOne: {
                    filter: { path: endpoint.path },
                    update: { $set: endpoint },
                    upsert: true,
                },
            })));
            res.send(data);
        }
        catch (e) {
            this.params.handleHttpError(res, e);
        }
    }
}
exports.EndpointsController = EndpointsController;
