Skip to content
This repository was archived by the owner on May 4, 2020. It is now read-only.

fix: bug visitor + giga opti tournamentFull #125

Merged
merged 1 commit into from
Nov 28, 2019
Merged
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
27 changes: 4 additions & 23 deletions src/api/controllers/cart/addItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Op } = require('sequelize');
const errorHandler = require('../../utils/errorHandler');
const validateBody = require('../../middlewares/validateBody');
const tshirtStocks = require('../../utils/tshirtStocks');
const { isTournamentFull } = require('../../utils/isFull');
const { isUserTournamentFull } = require('../../utils/isFull');

const ITEM_PLAYER_ID = 1;
const ITEM_VISITOR_ID = 2;
Expand Down Expand Up @@ -40,8 +40,7 @@ const CheckAddItem = [
*
* }
*/
const AddItemToCart = (cartIdString, cartItemModel, userModel, cartModel, teamModel, tournamentModel) => async (req, res) => {

const AddItemToCart = (cartIdString, cartItemModel, userModel, cartModel, teamModel, tournamentModel, itemModel) => async (req, res) => {

const cartId = req.params[cartIdString];

Expand Down Expand Up @@ -85,25 +84,7 @@ const AddItemToCart = (cartIdString, cartItemModel, userModel, cartModel, teamMo
// Attention: pas de verification d'attribute si ça peut correspondre à un itemId
// Est-ce utile ?
if (itemId === ITEM_PLAYER_ID) {
const forUser = await userModel.findByPk(req.body.forUserId, {
include: [
{
model: teamModel,
include: [
{
model: tournamentModel,
include: [
{
model: teamModel,
include: [userModel],
},
],
},
],
},
],
});
const isFull = await isTournamentFull(forUser.team.tournament, req);
const isFull = await isUserTournamentFull(req.body.forUserId, userModel, teamModel, tournamentModel, cartItemModel, cartModel);
if (isFull) {
return res
.status(400)
Expand All @@ -113,7 +94,7 @@ const AddItemToCart = (cartIdString, cartItemModel, userModel, cartModel, teamMo
}

if (itemId === ITEM_VISITOR_ID) {
const visitorItem = await Item.findByPk(ITEM_VISITOR_ID);
const visitorItem = await itemModel.findByPk(ITEM_VISITOR_ID);

const maxVisitors = visitorItem.stock;

Expand Down
2 changes: 1 addition & 1 deletion src/api/controllers/cart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Cart = (models) => {
`/:${cartId}/add`,
isAuth(),
CheckAddItem,
AddItemToCart(cartId, models.CartItem, models.User, models.Cart, models.Team, models.Tournament),
AddItemToCart(cartId, models.CartItem, models.User, models.Cart, models.Team, models.Tournament, models.Item),
);
router.put(
`/:${cartId}/cartItems/:${itemId}`,
Expand Down
15 changes: 2 additions & 13 deletions src/api/controllers/team/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,9 @@ const CheckCreate = [
* @param {object} teamModel
* @param {object} userModel
*/
const Create = (tournamentModel, teamModel, userModel) => async (req, res) => {
const Create = (tournamentModel, teamModel, userModel, cartItemModel, cartModel) => async (req, res) => {
try {
const tournament = await tournamentModel.findByPk(
req.body.tournament,
{
include: [
{
model: teamModel,
include: [userModel],
},
],
},
);
const tournamentFull = await isTournamentFull(tournament, req);
const [tournamentFull, tournament] = await isTournamentFull(req.body.tournament, userModel, teamModel, tournamentModel, cartItemModel, cartModel);
if (tournamentFull) {
return res
.status(400)
Expand Down
2 changes: 1 addition & 1 deletion src/api/controllers/team/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Team = (models) => {
router.post(
'/',
[isNotInTeam(), CheckCreate],
Create(models.Tournament, models.Team, models.User),
Create(models.Tournament, models.Team, models.User, models.CartItem, models.Cart),
);
router.delete(
`/:${teamId}`,
Expand Down
2 changes: 0 additions & 2 deletions src/api/controllers/tournament/list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const errorHandler = require('../../utils/errorHandler');
const hasTeamPaid = require('../../utils/hasTeamPaid.js');
const { op, literal, col } = require('sequelize');

/**
* Get all the tournaments
Expand Down
44 changes: 11 additions & 33 deletions src/api/controllers/user/payCart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const etupay = require('../../utils/etupay');

const errorHandler = require('../../utils/errorHandler');
const { isTournamentFull } = require('../../utils/isFull');
const { isUserTournamentFull } = require('../../utils/isFull');
const removeAccent = require('../../utils/removeAccents');

const ITEM_PLAYER_ID = 1;
Expand Down Expand Up @@ -119,39 +119,17 @@ const PayCart = (
(cartItem) => cartItem.item.id === ITEM_PLAYER_ID,
)
) {
await Promise.all(
cart.cartItems.map(async (cartItem) => {
if (cartItem.item.id === ITEM_PLAYER_ID) {
const team = await teamModel.findByPk(
req.user.teamId,
{
include: [
{
model: tournamentModel,
include: [
{
model: teamModel,
include: [userModel],
},
],
},
],
},
);
const isFull = await isTournamentFull(
team.tournament,
req,
);
if (isFull) {
return res
.status(400)
.json({ error: 'TOURNAMENT_FULL' })
.end();
}
for (const cartItem of cart.cartItems) {
if (cartItem.item.id === ITEM_PLAYER_ID) {
const isFull = await isUserTournamentFull(cartItem.forUserId, userModel, teamModel, tournamentModel, cartItemModel, cartModel);
if (isFull) {
return res
.status(400)
.json({ error: 'TOURNAMENT_FULL' })
.end();
}
return null;
}),
);
}
}
}

const data = JSON.stringify({ cartId: cart.id });
Expand Down
129 changes: 90 additions & 39 deletions src/api/utils/isFull.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,96 @@
const hasTeamPaid = require('./hasTeamPaid');
const { Op } = require('sequelize');

const isTeamFull = (team, max, paid = false) => {
let count;
if (!team.users) {
return false;
}

if (paid) {
count = team.users.filter((user) => user.paid).length;
}
else {
count = team.users.length;
}

return count >= max;
const isUserTournamentFull = async (id, userModel, teamModel, tournamentModel, cartItemModel, cartModel) => {
const includeCart = {
model: cartItemModel,
as: 'forUser',
attributes: [],
where: {
itemId: 1,
},
include: [
{
model: cartModel,
as: 'cart',
attributes: [],
where: {
transactionState: {
[Op.in]: ['paid', 'draft'],
},
},
},
],
};
const forUser = await userModel.findByPk(id, {
include: [
{
model: teamModel,
include: [
{
model: tournamentModel,
attributes: ['playersPerTeam', 'maxPlayers'],
include: [
{
model: teamModel,
attributes: ['id'],
include: {
model: userModel,
attributes: ['id'],
include: [includeCart],
},
},
],
},
],
},
],
});
const fullTeams = forUser.team.tournament.teams.filter((team) => team.users.length === forUser.team.tournament.playersPerTeam);
const maxTeams = forUser.team.tournament.maxPlayers / forUser.team.tournament.playersPerTeam;
return fullTeams.length >= maxTeams;
};

const isTournamentFull = async (tournament, req) => {
const isTournamentFull = async (id, userModel, teamModel, tournamentModel, cartItemModel, cartModel) => {
const includeCart = {
model: cartItemModel,
as: 'forUser',
attributes: [],
where: {
itemId: 1,
},
include: [
{
model: cartModel,
as: 'cart',
attributes: [],
where: {
transactionState: {
[Op.in]: ['paid', 'draft'],
},
},
},
],
};
const tournament = await tournamentModel.findByPk(
id,
{
attributes: ['playersPerTeam', 'maxPlayers'],
include: [
{
model: teamModel,
attributes: ['id'],
include: {
model: userModel,
attributes: ['id'],
include: [includeCart],
},
},
],
},
);
const fullTeams = tournament.teams.filter((team) => team.users.length === tournament.playersPerTeam);
const maxTeams = tournament.maxPlayers / tournament.playersPerTeam;
if (!tournament.teams) {
return false;
}
let teams = await Promise.all(tournament.teams.map(async (team) => {
let isPaid = true;
isPaid = await hasTeamPaid(req, team, null, tournament.playersPerTeam);
return (isPaid ? 'paid' : 'empty');
}));
teams = teams.filter((team) => team !== 'empty');
return teams.length >= maxTeams;
};

const remainingPlaces = (spotlight) => {
const maxTeams = spotlight.maxPlayers / spotlight.perTeam;
if (!spotlight.teams) {
return false;
}
const teams = spotlight.teams.filter((team) => isTeamFull(team, spotlight.perTeam, true)).length;

const remaining = maxTeams - teams;

return remaining <= 5 && remaining > 0 ? remaining : '/';
return [fullTeams.length >= maxTeams, tournament];
};

module.exports = { isTeamFull, isTournamentFull, remainingPlaces };
module.exports = { isUserTournamentFull, isTournamentFull };
61 changes: 0 additions & 61 deletions src/api/utils/isInTournament.js

This file was deleted.