"use strict";
(() => {
var exports = {};
exports.id = 841;
exports.ids = [841];
exports.modules = {

/***/ 2748:
/***/ ((module) => {

module.exports = require("luxon");

/***/ }),

/***/ 1185:
/***/ ((module) => {

module.exports = require("mongoose");

/***/ }),

/***/ 7773:
/***/ ((module) => {

module.exports = require("winston");

/***/ }),

/***/ 7564:
/***/ ((module) => {

module.exports = import("chalk");;

/***/ }),

/***/ 6555:
/***/ ((module) => {

module.exports = import("uuid");;

/***/ }),

/***/ 5053:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(8350);
/* harmony import */ var uuid__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6555);
/* harmony import */ var rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4969);
/* harmony import */ var rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(3452);
/* harmony import */ var rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(1051);
/* harmony import */ var winston_logger__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(674);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__, uuid__WEBPACK_IMPORTED_MODULE_1__, rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__, rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__, rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__, winston_logger__WEBPACK_IMPORTED_MODULE_5__]);
([rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__, uuid__WEBPACK_IMPORTED_MODULE_1__, rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__, rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__, rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__, winston_logger__WEBPACK_IMPORTED_MODULE_5__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);






const initAppId = "00000000-0000-0000-0000-000000000000";
async function handler(req, res) {
    var ref, ref1;
    const headerAppId = Array.isArray(req.headers["x-taskless-app-id"]) ? req.headers["x-taskless-app-id"][0] : req.headers["x-taskless-app-id"];
    const appId = headerAppId ? headerAppId : initAppId;
    const context = {
        v5: (value)=>(0,uuid__WEBPACK_IMPORTED_MODULE_1__.v5)(value, appId)
    };
    winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`Received request for Application ID ${appId}`);
    // success operation
    const success = (v)=>{
        res.status(200).json({
            data: v
        });
    };
    const error = (v)=>{
        winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.error */ .k.error(v);
        res.status(500).json({
            error: v,
            data: null
        });
    };
    try {
        if ((0,rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__/* .isEnqueueJob */ .K)(req.body)) {
            const v = await (0,rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__/* .enqueueJob */ .C)(req.body.variables, context);
            winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`RPC Enqueue Job: ${req.body.variables.name}`);
            return success(v);
        }
        if ((0,rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__/* .isUpdateJob */ .f)(req.body)) {
            const v = await (0,rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__/* .updateJob */ .g)(req.body.variables, context);
            winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`RPC Update Job: ${req.body.variables.name}`);
            return success(v);
        }
        if ((0,rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__/* .isDeleteJob */ .I)(req.body)) {
            const v = await (0,rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__/* .deleteJob */ .C)(req.body.variables, context);
            winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`RPC Delete Job: ${req.body.variables.name}`);
            return success(v);
        }
        if ((0,rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__/* .isGetJobByName */ .k)(req.body)) {
            const v = await (0,rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__/* .getJobByName */ .d)(req.body.variables, context);
            winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`RPC Get Job: ${req.body.variables.name}`);
            return success(v);
        }
    } catch (e) {
        if (process.env.TASKLESS_FULL_ERRORS === "1") {
            console.error(e);
        }
        return error(e);
    }
    winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.error */ .k.error("Unsupported RPC method: " + (((ref = req.body) === null || ref === void 0 ? void 0 : ref.method) ?? "unknown"));
    res.status(500).json({
        data: null,
        error: "Unsupported RPC method: " + (((ref1 = req.body) === null || ref1 === void 0 ? void 0 : ref1.method) ?? "unknown")
    });
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 5357:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "e": () => (/* binding */ gqlHeadersToObject)
/* harmony export */ });
const gqlHeadersToObject = (gqlHeaders)=>{
    if (typeof gqlHeaders === "undefined" || gqlHeaders === null) {
        return {};
    }
    const all = {};
    for (const h of gqlHeaders){
        all[h.name] = h.value;
    }
    return all;
};


/***/ }),

/***/ 4969:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "C": () => (/* binding */ deleteJob),
/* harmony export */   "I": () => (/* binding */ isDeleteJob)
/* harmony export */ });
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9306);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([mongo_db__WEBPACK_IMPORTED_MODULE_0__]);
mongo_db__WEBPACK_IMPORTED_MODULE_0__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];

const isDeleteJob = (v)=>{
    return v.method === "deleteJob";
};
const deleteJob = async (variables, context)=>{
    const id = context.v5(variables.name);
    const job = await mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .Job.findOneAndDelete */ .oI.findOneAndDelete({
        v5id: {
            $eq: id
        }
    }, {
        returnDocument: "before"
    }).exec();
    if (!job) {
        return {
            deleteJob: null
        };
    }
    return {
        deleteJob: {
            ...(0,mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .jobToJobFragment */ .dT)(variables.name, job)
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 1051:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "C": () => (/* binding */ enqueueJob),
/* harmony export */   "K": () => (/* binding */ isEnqueueJob)
/* harmony export */ });
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2748);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(luxon__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(9306);
/* harmony import */ var _common__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5357);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([mongo_db__WEBPACK_IMPORTED_MODULE_1__]);
mongo_db__WEBPACK_IMPORTED_MODULE_1__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];



const isEnqueueJob = (v)=>{
    return v.method === "enqueueJob";
};
const enqueueJob = async (variables, context)=>{
    const id = context.v5(variables.name);
    const runAt = variables.job.runAt ?? luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.now().toISO();
    const job = await mongo_db__WEBPACK_IMPORTED_MODULE_1__/* .Job.findOneAndUpdate */ .oI.findOneAndUpdate({
        v5id: {
            $eq: id
        }
    }, {
        $set: {
            body: variables.job.body ?? undefined,
            endpoint: variables.job.endpoint,
            headers: (0,_common__WEBPACK_IMPORTED_MODULE_2__/* .gqlHeadersToObject */ .e)(variables.job.headers),
            name: variables.name,
            retries: variables.job.retries === 0 ? 0 : variables.job.retries ?? 5,
            runAt,
            runEvery: variables.job.runEvery ?? undefined,
            updatedAt: new Date(),
            v5id: id,
            schedule: {
                next: luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.fromISO(runAt).toJSDate()
            }
        }
    }, {
        returnDocument: "after",
        upsert: true
    }).exec();
    if (!job) {
        throw new Error("No job could be created or updated");
    }
    return {
        replaceJob: {
            ...(0,mongo_db__WEBPACK_IMPORTED_MODULE_1__/* .jobToJobFragment */ .dT)(variables.name, job)
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 3452:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "d": () => (/* binding */ getJobByName),
/* harmony export */   "k": () => (/* binding */ isGetJobByName)
/* harmony export */ });
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9306);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([mongo_db__WEBPACK_IMPORTED_MODULE_0__]);
mongo_db__WEBPACK_IMPORTED_MODULE_0__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];

const isGetJobByName = (v)=>{
    return v.method === "getJobByName";
};
const getJobByName = async (variables, context)=>{
    const id = context.v5(variables.name);
    const job = await mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .Job.findOne */ .oI.findOne({
        v5id: {
            $eq: id
        }
    }).exec();
    if (!job) {
        return null;
    }
    return {
        job: {
            ...(0,mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .jobToJobFragment */ .dT)(job.name, job)
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 8350:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "f": () => (/* binding */ isUpdateJob),
/* harmony export */   "g": () => (/* binding */ updateJob)
/* harmony export */ });
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9306);
/* harmony import */ var _common__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5357);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2748);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(luxon__WEBPACK_IMPORTED_MODULE_1__);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([mongo_db__WEBPACK_IMPORTED_MODULE_0__]);
mongo_db__WEBPACK_IMPORTED_MODULE_0__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];



const isUpdateJob = (v)=>{
    return v.method === "updateJob";
};
const updateJob = async (variables, context)=>{
    const id = context.v5(variables.name);
    const next = {};
    if (variables.job.body) {
        next.body = variables.job.body;
    }
    if (variables.job.endpoint) {
        next.endpoint = variables.job.endpoint;
    }
    if (typeof variables.job.enabled === "boolean") {
        next.enabled = variables.job.enabled;
    }
    if (variables.job.headers) {
        next.headers = (0,_common__WEBPACK_IMPORTED_MODULE_2__/* .gqlHeadersToObject */ .e)(variables.job.headers);
    }
    if (typeof variables.job.retries !== "undefined" && variables.job.retries !== null) {
        next.retries = variables.job.retries;
    }
    if (variables.job.runAt) {
        next.runAt = variables.job.runAt;
        next.schedule = new mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .Schedule */ .Pf({
            next: luxon__WEBPACK_IMPORTED_MODULE_1__.DateTime.fromISO(variables.job.runAt).toJSDate(),
            attempt: 0
        });
    }
    if (variables.job.runEvery) {
        next.runEvery = variables.job.runEvery;
    }
    next.updatedAt = new Date();
    const job = await mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .Job.findOneAndUpdate */ .oI.findOneAndUpdate({
        v5id: {
            $eq: id
        }
    }, {
        $set: next
    }, {
        returnDocument: "after",
        upsert: false
    }).exec();
    if (!job) {
        throw new Error("No job could be created or updated");
    }
    return {
        updateJob: {
            ...(0,mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .jobToJobFragment */ .dT)(variables.name, job)
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [306,674], () => (__webpack_exec__(5053)));
module.exports = __webpack_exports__;

})();