7c0e94d58dbce5a071d997c642f893029a94d2ec — Martin Angers 5 months ago 501f9d0
add validation during league archival
1 files changed, 11 insertions(+), 2 deletions(-)

M src/leagues.js
M src/leagues.js => src/leagues.js +11 -2
@@ 7,8 7,17 @@ export async function createLeague (conn, name, state = LeagueState.active) {
 }
 
 export async function archiveLeague (conn, leagueId) {
-  await conn.query ('update leagues set state = ? where id = ?',
-    [LeagueState.archived, leagueId])
+  await conn.query (`
+    update leagues l
+    set    state = ?
+    where  id = ?
+    and    not exists (
+      select 1
+      from   seasons s
+      where  s.league_id = l.id
+      and    s.state != ?
+    )
+  `, [LeagueState.archived, leagueId, SeasonState.completed])
 }
 
 // returns the list of leagues corresponding to the criteria