"use strict";
exports.id = 6791;
exports.ids = [6791];
exports.modules = {

/***/ 908690:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.jsonbSet = void 0;
const misc_1 = __webpack_require__(854006);
/*
jsonbSet: This little piece of very hard to write (and clever?) code
makes it so we can set or **merge in at any nested level** (!)
arbitrary JSON objects.  We can also delete any key at any
level by making the value null or undefined!  This is amazingly
easy to use in queries -- basically making JSONP with postgres
as expressive as RethinkDB REQL (even better in some ways).
*/
// The input desc is an object that describes what is being set, e.g.,
//     account_id = '... some uuid ';
//     desc = {"users": {[account_id]:{group:'collaborator'}}}
// changes the users JSONB field of the table so that users[account_id] has
// the group set to "collaborator".
// If the merge field is set then we merge in the change; otherwise,
// we replace the value.
// IMPORTANT: this is a dangerous attack vector -- do not call this function
// with unsanitized input from a user!
function jsonbSet(desc, merge = false) {
    const params = [];
    function pushParam(val, type) {
        if (type.toUpperCase() == "JSONB") {
            val = JSON.stringify(val); // this is needed by the driver....}
        }
        params.push(val);
        return params.length;
    }
    function set(field, data, path) {
        let obj = `COALESCE(${field}#>'{${path.join(",")}}', '{}'::JSONB)`;
        for (const key in data) {
            const val = data[key];
            if (val == null) {
                // remove key from object
                obj = `(${obj} - '${key}')`;
            }
            else {
                // set key in object
                if (merge && typeof val === "object" && !(0, misc_1.is_date)(val)) {
                    const subobj = set(field, val, path.concat([key]));
                    obj = `JSONB_SET(${obj}, '{${key}}', ${subobj})`;
                }
                else {
                    // completely replace field[key] with val.
                    obj = `JSONB_SET(${obj}, '{${key}}', $${pushParam(val, "JSONB")}::JSONB)`;
                }
            }
        }
        return obj;
    }
    const v = [];
    for (const field in desc) {
        const data = desc[field];
        v.push(`${field}=${set(field, data, [])}`);
    }
    return { set: v.join(","), params };
}
exports.jsonbSet = jsonbSet;
//# sourceMappingURL=jsonb-utils.js.map

/***/ }),

/***/ 786791:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


// Do all the account creation actions for the given account.  This should be called
// immediately after creating the account.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.creationActionsDone = void 0;
const pool_1 = __importDefault(__webpack_require__(541808));
const add_user_to_project_1 = __importDefault(__webpack_require__(159897));
async function accountCreationActions(email_address, account_id) {
    const pool = (0, pool_1.default)();
    const { rows } = await pool.query("SELECT action FROM account_creation_actions WHERE email_address=$1 AND expire > NOW()", [email_address]);
    for (const { action } of rows) {
        if (action.action == "add_to_project") {
            const { project_id, group } = action;
            await (0, add_user_to_project_1.default)({ project_id, account_id, group });
        }
        else {
            throw Error(`unknown account creation action "${action.action}"`);
        }
    }
}
exports["default"] = accountCreationActions;
async function creationActionsDone(account_id) {
    const pool = (0, pool_1.default)();
    await pool.query("UPDATE accounts SET creation_actions_done=true WHERE account_id=$1::UUID", [account_id]);
}
exports.creationActionsDone = creationActionsDone;
//# sourceMappingURL=account-creation-actions.js.map

/***/ }),

/***/ 159897:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const pool_1 = __importDefault(__webpack_require__(541808));
const jsonb_utils_1 = __webpack_require__(908690);
const misc_1 = __webpack_require__(854006);
async function addUserToProject({ account_id, project_id, group, // default is 'collaborator'
 }) {
    if (!(0, misc_1.is_valid_uuid_string)(account_id) || !(0, misc_1.is_valid_uuid_string)(project_id)) {
        throw Error("account_id and project_id must be UUID's");
    }
    const pool = (0, pool_1.default)();
    if (!group) {
        group = "collaborator";
    }
    const { set, params } = (0, jsonb_utils_1.jsonbSet)({ users: { [account_id]: { group } } });
    await pool.query(`UPDATE projects SET ${set} WHERE project_id=$${params.length + 1}`, params.concat(project_id));
}
exports["default"] = addUserToProject;
//# sourceMappingURL=add-user-to-project.js.map

/***/ })

};
;