2021-04-19 12:34:07 +02:00
|
|
|
const { newid } = require("../hashing")
|
2021-04-19 18:31:47 +02:00
|
|
|
|
|
|
|
exports.ViewNames = {
|
|
|
|
USER_BY_EMAIL: "by_email",
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
exports.StaticDatabases = {
|
2021-04-16 19:09:34 +02:00
|
|
|
GLOBAL: {
|
|
|
|
name: "global-db",
|
2021-04-07 12:33:16 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const DocumentTypes = {
|
|
|
|
USER: "us",
|
2021-04-13 14:26:13 +02:00
|
|
|
APP: "app",
|
2021-04-19 12:34:07 +02:00
|
|
|
GROUP: "group",
|
2021-04-21 17:42:44 +02:00
|
|
|
TEMPLATE: "template",
|
2021-04-07 12:33:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 12:20:37 +02:00
|
|
|
exports.DocumentTypes = DocumentTypes
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
const UNICODE_MAX = "\ufff0"
|
|
|
|
const SEPARATOR = "_"
|
|
|
|
|
2021-04-13 14:26:13 +02:00
|
|
|
exports.SEPARATOR = SEPARATOR
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
/**
|
2021-04-19 12:34:07 +02:00
|
|
|
* Generates a new group ID.
|
|
|
|
* @returns {string} The new group ID which the group doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateGroupID = () => {
|
|
|
|
return `${DocumentTypes.GROUP}${SEPARATOR}${newid()}`
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
/**
|
2021-04-19 17:16:46 +02:00
|
|
|
* Gets parameters for retrieving groups.
|
|
|
|
*/
|
|
|
|
exports.getGroupParams = (id = "", otherProps = {}) => {
|
|
|
|
return {
|
|
|
|
...otherProps,
|
|
|
|
startkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}`,
|
|
|
|
endkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}${UNICODE_MAX}`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:17:44 +02:00
|
|
|
/**
|
|
|
|
* Generates a new global user ID.
|
|
|
|
* @returns {string} The new user ID which the user doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateGlobalUserID = () => {
|
|
|
|
return `${DocumentTypes.USER}${SEPARATOR}${newid()}`
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:16:46 +02:00
|
|
|
/**
|
|
|
|
* Gets parameters for retrieving users.
|
2021-04-07 12:33:16 +02:00
|
|
|
*/
|
2021-04-21 17:42:44 +02:00
|
|
|
exports.getGlobalUserParams = (globalId, otherProps = {}) => {
|
2021-04-19 18:31:47 +02:00
|
|
|
if (!globalId) {
|
|
|
|
globalId = ""
|
2021-04-09 16:11:49 +02:00
|
|
|
}
|
2021-04-07 12:33:16 +02:00
|
|
|
return {
|
|
|
|
...otherProps,
|
2021-04-19 18:31:47 +02:00
|
|
|
startkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}`,
|
|
|
|
endkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}${UNICODE_MAX}`,
|
2021-04-07 12:33:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 17:42:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a template ID.
|
|
|
|
* @param ownerId The owner/user of the template, this could be global or a group level.
|
|
|
|
*/
|
|
|
|
exports.generateTemplateID = ownerId => {
|
|
|
|
return `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}${newid()}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets parameters for retrieving templates. Owner ID must be specified, either global or a group level.
|
|
|
|
*/
|
|
|
|
exports.getTemplateParams = (ownerId, templateId, otherProps = {}) => {
|
|
|
|
if (!templateId) {
|
|
|
|
templateId = ""
|
|
|
|
}
|
|
|
|
const base = `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}`
|
|
|
|
const final = templateId ? `${base}${SEPARATOR}${templateId}` : base
|
|
|
|
return {
|
|
|
|
...otherProps,
|
|
|
|
startkey: final,
|
|
|
|
endkey: `${final}${UNICODE_MAX}`,
|
|
|
|
}
|
|
|
|
}
|