(() => {
var exports = {};
exports.id = 886;
exports.ids = [886];
exports.modules = {

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

"use strict";
module.exports = require("assert");

/***/ }),

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

"use strict";
module.exports = require("buffer");

/***/ }),

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

"use strict";
module.exports = require("crypto");

/***/ }),

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

"use strict";
module.exports = require("dns");

/***/ }),

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

"use strict";
module.exports = require("events");

/***/ }),

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

"use strict";
module.exports = require("fs");

/***/ }),

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

"use strict";
module.exports = require("http");

/***/ }),

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

"use strict";
module.exports = require("net");

/***/ }),

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

"use strict";
module.exports = require("os");

/***/ }),

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

"use strict";
module.exports = require("path");

/***/ }),

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

"use strict";
module.exports = require("process");

/***/ }),

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

"use strict";
module.exports = require("stream");

/***/ }),

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

"use strict";
module.exports = require("string_decoder");

/***/ }),

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

"use strict";
module.exports = require("tls");

/***/ }),

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

"use strict";
module.exports = require("tty");

/***/ }),

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

"use strict";
module.exports = require("url");

/***/ }),

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

"use strict";
module.exports = require("util");

/***/ }),

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

"use strict";
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "Z": () => (/* binding */ getParams)
/* harmony export */ });
function getParams(req) {
    if (req?.method == "POST") {
        return new Proxy({}, {
            get (_, key) {
                return req.body?.[key];
            }
        });
    /*
    // Disabled, since this could lead to a sneaky click on a link attack.
    // Should only be enabled for dev purposes.
    } else if (req?.method == "GET") {
    return new Proxy(
      {},
      {
        get(_, key) {
          if (typeof key != "string") {
            return undefined;
          }
          return req.query?.[key];
        },
      }
    );
*/ } else {
        // only support params for POST requests.
        return {};
    }
}


/***/ }),

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

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handle)
/* harmony export */ });
/* harmony import */ var _cocalc_server_shopping_cart_add__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(213653);
/* harmony import */ var _cocalc_server_shopping_cart_add__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_cocalc_server_shopping_cart_add__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var lib_account_get_account__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(559459);
/* harmony import */ var lib_api_get_params__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(26438);
/*
Add item to the shopping cart for signed in user.

request body:

- product and description: to add something new to the cart
or
- id: to move something back into the cart that was removed
*/ 


async function handle(req, res) {
    try {
        res.json(await add(req));
    } catch (err) {
        res.json({
            error: `${err.message}`
        });
        return;
    }
}
async function add(req) {
    const account_id = await (0,lib_account_get_account__WEBPACK_IMPORTED_MODULE_1__/* ["default"] */ .Z)(req);
    if (account_id == null) {
        throw Error("must be signed in to use shopping cart");
    }
    const { product , description , id , purchased , project_id  } = (0,lib_api_get_params__WEBPACK_IMPORTED_MODULE_2__/* ["default"] */ .Z)(req);
    if (id != null) {
        if (purchased) {
            // put copy of it in the cart
            return await (0,_cocalc_server_shopping_cart_add__WEBPACK_IMPORTED_MODULE_0__.buyItAgain)(account_id, id);
        } else {
            // adding something back to cart that was saved for later
            return await (0,_cocalc_server_shopping_cart_add__WEBPACK_IMPORTED_MODULE_0__.putBackInCart)(account_id, id);
        }
    }
    if (!product) {
        throw Error("if id isn't specified then the product must be set");
    }
    return await _cocalc_server_shopping_cart_add__WEBPACK_IMPORTED_MODULE_0___default()(account_id, product, description, project_id);
}


/***/ }),

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

"use strict";

/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.buyItAgain = exports.putBackInCart = void 0;
/*
Add an item to the given user's shopping cart.

TODO/WORRY -- a user could (on purpose or due to a bug) create an unbounded
number of items in their cart, which would be bad for the database.
We need to worry about that at some point, e.g., by throttling or
checking periodically, then blacklisting...?  This isn't something of
any value to a spammer so it's very unlikely to be exploited maliciously.
*/
const misc_1 = __webpack_require__(854006);
const pool_1 = __importDefault(__webpack_require__(541808));
const get_1 = __webpack_require__(494757);
async function addToCart(account_id, product, description, project_id) {
    if (!(0, misc_1.isValidUUID)(account_id)) {
        throw Error("account_id is invalid");
    }
    if (typeof project_id !== "string" || !(0, misc_1.isValidUUID)(project_id)) {
        project_id = undefined;
    }
    const pool = (0, pool_1.default)();
    const { rowCount } = await pool.query(`INSERT INTO shopping_cart_items (account_id, added, product, description, checked, project_id)
    VALUES($1, NOW(), $2, $3, true, $4)`, [account_id, product, description, project_id]);
    return rowCount;
}
exports["default"] = addToCart;
// Puts an item back in the cart that was removed.
// - Mutates item that was actually removed and not purchased.
async function putBackInCart(account_id, id) {
    if (!(0, misc_1.isValidUUID)(account_id)) {
        throw Error("account_id is invalid");
    }
    const pool = (0, pool_1.default)();
    const { rowCount } = await pool.query("UPDATE shopping_cart_items SET removed=NULL, checked=TRUE WHERE account_id=$1 AND id=$2 AND removed IS NOT NULL AND purchased IS NULL", [account_id, id]);
    return rowCount;
}
exports.putBackInCart = putBackInCart;
// Makes copy of item that was purchased and puts it in the cart.
async function buyItAgain(account_id, id) {
    if (!(0, misc_1.isValidUUID)(account_id)) {
        throw Error("account_id is invalid");
    }
    // this errors if it doesn't return 1 item.
    const item = await (0, get_1.getItem)({
        id,
        account_id,
    });
    await addToCart(account_id, item.product, item.description);
    return 1;
}
exports.buyItAgain = buyItAgain;
//# sourceMappingURL=add.js.map

/***/ }),

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

var crypto = __webpack_require__(706113);

var rn = function(max) {
  var rnBytes = crypto.randomBytes(2);
  var randomNum = rnBytes.readUInt8(0) * 256 + rnBytes.readUInt8(1);
  return randomNum % max;
}

var digits = '0123456789'
var base62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'
var base30 = '0123456789ABCDFHKLMNPQRSTUVWXYZ'  // 1-9, A-Z exclude(E, G, I, J, O) for human read

exports.generate = function(len, chars) {
  len = len || 16;
  chars = chars || base62;
  var key = '';
  var charsLen = chars.length;
  for (var i=0; i<len; i++) {
    key += chars[rn(charsLen)];
  }
  return key;
};

exports.generateDigits = function(len) {
  len = len || 16;
  return exports.generate(len, digits)
};

exports.generateBase30 = function(len) {
  len = len || 16;
  return exports.generate(len, base30)
}


/***/ })

};
;

// 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, [8180,2921,9557,5245,1808,4006,8312,5710,9459,4757], () => (__webpack_exec__(637004)));
module.exports = __webpack_exports__;

})();