Skip to content

Commit

Permalink
Fixed SQL query for detecting meal's payer
Browse files Browse the repository at this point in the history
  • Loading branch information
pavloniym committed Mar 18, 2018
1 parent 1ea420b commit 2e1c5d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion callbacks/meal/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async (DB, bot, {callback, chats_id, messages_id}) => {
/*
* Update meal's payer
*/
await DB.models.Meals.payer({meals_id: callback.meals_id});
await DB.models.Meals.payer({meals_id: callback.meals_id, chats_id});


/*
Expand Down
12 changes: 6 additions & 6 deletions proxies/Meals.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ export default class Meals {
/**
* Update payer
* @param meals_id
* @param chats_id
* @returns {Promise<*>}
*/
async payer({meals_id}) {
async payer({meals_id, chats_id}) {


const meal = await this.get({meals_id});
Expand All @@ -98,17 +99,16 @@ export default class Meals {
m.payers_id
from meals as m
inner join meals_members as mm on m.id = mm.meals_id and mm.members_id IN (:eaters)
inner join members as me on me.id = mm.members_id
inner join meals_members as mm on m.id = mm.meals_id
where m.id != :meals_id and m.confirmed = 1
where m.id != :meals_id and m.confirmed = 1 and m.chats_id = :chats_id
group by m.id
having eaters = :eaters_number
HAVING (SUM(mm.members_id NOT IN (:eaters)) = 0 AND SUM(mm.members_id IN (:eaters)) = :eaters_number)
order by m.createdAt desc
limit :meals_limit`,
{
replacements: {
chats_id,
eaters: eaters,
meals_id: meal.id,
eaters_number: eaters.length,
Expand Down

0 comments on commit 2e1c5d4

Please sign in to comment.