Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/multi seller auction query #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion services/news/src/views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@
}

async function setupDemo () {
if ('<%= isMultiSeller %>' !== 'true') {
const searchParams = new URLSearchParams(window.location.search);
const multiSellerSearchParams = searchParams.get('auctionType');

if (multiSellerSearchParams !== 'multi') {
document.getElementById('video-ad-container').style.display = 'none'
addScriptEl('<%= SSP_TAG_URL %>', 'ssp_tag')
return
Expand Down
249 changes: 113 additions & 136 deletions services/shop/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,182 +16,159 @@
import express from 'express';
import session from 'express-session';
import MemoryStoreFactory from 'memorystore';
import {
DSP_HOST,
DSP_A_HOST,
DSP_B_HOST,
EXTERNAL_PORT,
PORT,
SHOP_DETAIL,
SHOP_HOST,
} from './env.js';
import {
addOrder,
displayCategory,
fromSize,
getItem,
getItems,
removeOrder,
updateOrder,
} from './lib/items.js';
import { DSP_HOST, DSP_A_HOST, DSP_B_HOST, EXTERNAL_PORT, PORT, SHOP_DETAIL, SHOP_HOST, } from './env.js';
import { addOrder, displayCategory, fromSize, getItem, getItems, removeOrder, updateOrder, } from './lib/items.js';
const app = express();
app.set('trust proxy', 1); // required for Set-Cookie with Secure
// Due to express >= 4 changes, we need to pass express-session to the
// function memorystore exports in order to extend session.Store:
const MemoryStore = MemoryStoreFactory(session);
const oneDay = 1000 * 60 * 60 * 24;
app.use(
session({
app.use(session({
name: '__session',
secret: 'THIS IS SECRET FOR DEMO',
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
sameSite: 'strict',
maxAge: oneDay,
secure: true,
sameSite: 'strict',
maxAge: oneDay,
},
store: new MemoryStore({
checkPeriod: oneDay,
checkPeriod: oneDay,
}),
}),
);
}));
app.use((req, res, next) => {
// res.setHeader("Origin-Trial", NEWS_TOKEN as string)
res.setHeader('Cache-Control', 'private');
if (!req.session.cart) {
req.session.cart = [];
}
next();
// res.setHeader("Origin-Trial", NEWS_TOKEN as string)
res.setHeader('Cache-Control', 'private');
if (!req.session.cart) {
req.session.cart = [];
}
next();
});
app.use(express.urlencoded({extended: true}));
app.use(express.urlencoded({ extended: true }));
app.use(express.static('src/public'));
app.set('view engine', 'ejs');
app.set('views', 'src/views');
// view helper
app.locals = {
title: SHOP_DETAIL,
displayCategory,
register_trigger: (order) => {
const {item, size, quantity} = order;
const register_trigger = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}`);
register_trigger.pathname = '/register-trigger';
register_trigger.searchParams.append('id', item.id);
register_trigger.searchParams.append('category', `${item.category}`);
register_trigger.searchParams.append('quantity', `${quantity}`);
register_trigger.searchParams.append('size', `${fromSize(size)}`);
register_trigger.searchParams.append('gross', `${item.price * quantity}`);
return register_trigger.toString();
},
title: SHOP_DETAIL,
displayCategory,
register_trigger: (order) => {
const { item, size, quantity } = order;
const register_trigger = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}`);
register_trigger.pathname = '/register-trigger';
register_trigger.searchParams.append('id', item.id);
register_trigger.searchParams.append('category', `${item.category}`);
register_trigger.searchParams.append('quantity', `${quantity}`);
register_trigger.searchParams.append('size', `${fromSize(size)}`);
register_trigger.searchParams.append('gross', `${item.price * quantity}`);
return register_trigger.toString();
},
};
app.get('/', async (req, res) => {
const items = await getItems();
res.render('index', {
items,
});
const items = await getItems();
res.render('index', {
items,
});
});
// serves the static ads creative from shop site (redirected from ssp)
app.get('/ads/:id', async (req, res) => {
const id = req.params.id;
const imgPath = `/image/svg/emoji_u${id}.svg`;
//res.set("Content-Type", "image/svg+xml")
console.log(`redirecting to /image/svg/emoji_u${id}.svg`);
res.redirect(301, imgPath);
const id = req.params.id;
const imgPath = `/image/svg/emoji_u${id}.svg`;
//res.set("Content-Type", "image/svg+xml")
console.log(`redirecting to /image/svg/emoji_u${id}.svg`);
res.redirect(301, imgPath);
});
app.get('/items/:id', async (req, res) => {
const {id} = req.params;
const item = await getItem(id);
const isMultiSeller = req.query.auctionType === 'multi';
const DSP_TAG_URL = new URL(
`https://${DSP_HOST}:${EXTERNAL_PORT}/dsp-tag.js`,
);
const DSP_A_TAG_URL = new URL(
`https://${DSP_A_HOST}:${EXTERNAL_PORT}/dsp-tag.js`,
);
const DSP_B_TAG_URL = new URL(
`https://${DSP_B_HOST}:${EXTERNAL_PORT}/dsp-tag.js`,
);
res.render('item', {
item,
DSP_TAG_URL,
DSP_A_TAG_URL,
DSP_B_TAG_URL,
SHOP_HOST,
isMultiSeller,
});
const { id } = req.params;
const item = await getItem(id);
const isMultiSeller = req.query.auctionType === 'multi';
const DSP_TAG_URL = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}/dsp-tag.js`);
const DSP_A_TAG_URL = new URL(`https://${DSP_A_HOST}:${EXTERNAL_PORT}/dsp-tag.js`);
const DSP_B_TAG_URL = new URL(`https://${DSP_B_HOST}:${EXTERNAL_PORT}/dsp-tag.js`);
res.render('item', {
item,
DSP_TAG_URL,
DSP_A_TAG_URL,
DSP_B_TAG_URL,
SHOP_HOST,
isMultiSeller,
});
});
app.post('/cart', async (req, res, next) => {
const {id, size, quantity} = req.body;
const item = await getItem(id);
const order = {item, size, quantity};
const cart = addOrder(order, req.session.cart);
req.session.cart = cart;
// save the session before redirection to ensure page
// load does not happen before session is saved
req.session.save(function (err) {
if (err) return next(err);
// console.log("Save session before redirect")
// console.log(req.session)
res.redirect(303, '/cart');
});
//res.redirect(303, "/cart")
const { id, size, quantity } = req.body;
const item = await getItem(id);
const order = { item, size, quantity };
const cart = addOrder(order, req.session.cart);
req.session.cart = cart;
// save the session before redirection to ensure page
// load does not happen before session is saved
req.session.save(function (err) {
if (err)
return next(err);
// console.log("Save session before redirect")
// console.log(req.session)
res.redirect(303, '/cart');
});
//res.redirect(303, "/cart")
});
app.get('/cart', async (req, res) => {
const cart = req.session.cart;
const subtotal = cart.reduce((sum, {item, quantity}) => {
return sum + item.price * quantity;
}, 0);
const shipping = 40;
res.render('cart', {
cart,
subtotal,
shipping,
});
const cart = req.session.cart;
const subtotal = cart.reduce((sum, { item, quantity }) => {
return sum + item.price * quantity;
}, 0);
const shipping = 40;
res.render('cart', {
cart,
subtotal,
shipping,
});
});
app.put('/cart/:name', async (req, res) => {
const {name} = req.params;
const {quantity} = req.body;
const [id, size] = name.split(':');
const item = await getItem(id);
const order = {
item,
size,
quantity,
};
const cart = updateOrder(order, req.session.cart);
req.session.cart = cart;
res.status(204).end();
const { name } = req.params;
const { quantity } = req.body;
const [id, size] = name.split(':');
const item = await getItem(id);
const order = {
item,
size,
quantity,
};
const cart = updateOrder(order, req.session.cart);
req.session.cart = cart;
res.status(204).end();
});
app.delete('/cart/:name', async (req, res) => {
const {name} = req.params;
const [id, size] = name.split(':');
const item = await getItem(id);
const order = {
item,
size: size,
quantity: 0,
};
const cart = removeOrder(order, req.session.cart);
req.session.cart = cart;
res.status(204).end();
const { name } = req.params;
const [id, size] = name.split(':');
const item = await getItem(id);
const order = {
item,
size: size,
quantity: 0,
};
const cart = removeOrder(order, req.session.cart);
req.session.cart = cart;
res.status(204).end();
});
app.post('/checkout', async (req, res) => {
const body = req.body;
res.redirect(303, '/checkout');
const body = req.body;
res.redirect(303, '/checkout');
});
app.get('/checkout', async (req, res) => {
const cart = req.session.cart;
const subtotal = cart.reduce((sum, {item, quantity}) => {
return sum + item.price * quantity;
}, 0);
const shipping = 40;
await req.session.destroy(() => Promise.resolve());
res.render('checkout', {
cart,
subtotal,
shipping,
});
const cart = req.session.cart;
const subtotal = cart.reduce((sum, { item, quantity }) => {
return sum + item.price * quantity;
}, 0);
const shipping = 40;
await req.session.destroy(() => Promise.resolve());
res.render('checkout', {
cart,
subtotal,
shipping,
});
});
app.listen(PORT, async () => {
console.log(`Listening on port ${PORT}`);
console.log(`Listening on port ${PORT}`);
});
Loading