You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.3 KiB
JavaScript

const pg = require('../pg')
const joinjs = require('join-js').default;
const debug = require('debug')('sos:db:session')
const mappings = require('../mappings')
const session = module.exports = {}
session.create = async (req, _user) => {
const user = _user ? _user : req.user
const session = await createSessionInternal(
user ? user.uuid : null,
req.ip,
req.get('User Agent') || "",
req.get('Referrer') || ""
)
req.session.uuid = session.uuid
req.sessionObj = session
return session
}
const createSessionInternal = async (user_uuid, ip_address, user_agent, referer) => {
const query = {
text: 'select * from sos.login_user_session($1, $2, $3, $4, $5)',
values: [
user_uuid,
'2 hours',
ip_address,
user_agent,
referer
]
}
debug(query);
const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'sessionMap', 'session_')[0];
}
session.validate = async (session_uuid) => {
const query = {
text: 'select * from sos.validate_session($1)',
values: [
session_uuid
]
}
debug(query);
const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'sessionMap', 'session_')[0];
}
session.update = async (session_uuid) => {
const query = {
text: 'select * from sos.update_session($1)',
values: [
session_uuid
]
}
debug(query);
const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'sessionMap', 'session_')[0];
}
session.end = async (session_uuid) => {
const query = {
text: 'select * from sos.end_session($1)',
values: [
session_uuid
]
}
debug(query);
const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'sessionMap', 'session_')[0];
}
session.attachCart = async (session_uuid, cart_uuid) => {
const query = {
text: 'select * from sos.attach_cart($1, $2)',
values: [
session_uuid,
cart_uuid
]
}
debug(query);
const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'sessionMap', 'session_')[0];
}
session.clearCart = async (session_uuid) => {
const query = {
text: 'select * from sos.clear_cart($1)',
values: [session_uuid]
}
debug(query);
const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'sessionMap', 'session_')[0];
}