diff --git a/app/SeedWeeklyPlayerData.patch.js b/app/SeedWeeklyPlayerData.patch.js index 8fb5e4f..22073c3 100644 --- a/app/SeedWeeklyPlayerData.patch.js +++ b/app/SeedWeeklyPlayerData.patch.js @@ -33,6 +33,9 @@ class SeedWeeklyPlayerDataPatch extends Injectable { // Clear existing data await WeeklyPlayerStat.deleteMany() + // Array of players with week 1 stats + const player_ids_with_week_1_stats = [] + // Populate the weekly player stats for all weeks in the range for ( let week = start_week; week <= end_week; week += 1 ) { this.output.info(`Building weekly player stats for week ${week}...`) @@ -61,6 +64,10 @@ class SeedWeeklyPlayerDataPatch extends Injectable { await weekly_stat.save() + if ( week === 1 ) { + player_ids_with_week_1_stats.push(player.id) + } + if ( week === 1 || !player.seed_stats || Object.values(player.seed_stats).length < 1 ) { player.seed_stats = await weekly_stat.to_api() } @@ -71,6 +78,19 @@ class SeedWeeklyPlayerDataPatch extends Injectable { this.output.success(` - complete`) } + this.output.info('Deactivating players without week 1 stats...') + const inactive_players = await Player.find({ + _id: { + $nin: player_ids_with_week_1_stats.map(x => Player.to_object_id(x)), + }, + }) + + this.output.info(`Deactivating ${inactive_players.length} players...`) + for ( const player of inactive_players ) { + player.is_active = false + await player.save() + } + this.output.success('Complete!') } } diff --git a/app/models/Player.model.js b/app/models/Player.model.js index b1c248d..a552e7e 100644 --- a/app/models/Player.model.js +++ b/app/models/Player.model.js @@ -1,5 +1,5 @@ const { Model } = require('flitter-orm') - +const ActiveScope = require('./scopes/Active.scope') /* * Player Model @@ -10,6 +10,8 @@ class Player extends Model { return [...super.services, 'output', 'models', 'sports_data'] } + static scopes = [new ActiveScope()] + /* * Define the flitter-orm schema of the model. */ @@ -37,6 +39,10 @@ class Player extends Model { photo_url: String, seed_stats: Object, + + // False if the player doesn't have any week-1 stats. + // If so, they will be hidden to make the game more playable. + is_active: { type: Boolean, default: true }, } } diff --git a/app/models/scopes/Active.scope.js b/app/models/scopes/Active.scope.js new file mode 100644 index 0000000..d485642 --- /dev/null +++ b/app/models/scopes/Active.scope.js @@ -0,0 +1,9 @@ +const Scope = require('flitter-orm/src/model/Scope') + +class ActiveScope extends Scope { + async filter(to_filter) { + return to_filter.equal('is_active', true) + } +} + +module.exports = exports = ActiveScope diff --git a/test/backend/framework.js b/test/backend/framework.js new file mode 100644 index 0000000..e9347da --- /dev/null +++ b/test/backend/framework.js @@ -0,0 +1,38 @@ + +// boot the framework for use in testing +const framework$ = (function() { + class FrameworkLoader { + listeners = [] + booted = false + + subscribe(handler) { + if ( !this.booted ) this.listeners.push(handler) + else handler() + } + + boot() { + const units = require('../../Units.flitter') + delete units.App + const { FlitterApp, RunLevelErrorHandler } = require('libflitter') + this.flitter = new FlitterApp(units) + this.rleh = new RunLevelErrorHandler() + + this.flitter.up().then(() => { + this.booted = true + for ( const listener of this.listeners ) { + try { + listener() + } catch (e) {} + } + }) + } + + stop() { + return this.flitter.down() + } + } + + return new FrameworkLoader() +})() + +module.exports = exports = framework$ diff --git a/test/backend/models_Player.spec.js b/test/backend/models_Player.spec.js new file mode 100644 index 0000000..dbc3377 --- /dev/null +++ b/test/backend/models_Player.spec.js @@ -0,0 +1,43 @@ +const { expect } = require('chai') +const sinon = require('sinon') +const Player = require('../../app/models/Player.model') +const { Model } = require('flitter-orm') + +describe('the player model', function() { + it('should extend Model', function() { + expect(new Player).to.be.an.instanceOf(Model) + }) + + it('should format players for the API', async function() { + const player = new Player({ + patch_data: { + patch_team_id: 4, + patch_team_name: 'Test team', + patch_team_key: 'TTM', + }, + player_number: 34, + first_name: 'Test', + last_name: 'Player', + full_name: 'A Test Player', + position: 'QB', + fantasy_position: 'QB', + height: '6\'4"', + weight: 250, + birthday: '2020-11-11', + experience: 0, + experience_string: 'rookie', + age: 21, + photo_url: 'http://image.com/img.png', + }) + + expect(await player.to_api()).to.be.eql({ + id: undefined, + number: 34, + name: 'A Test Player', + position: 'QB', + team_name: 'Test team', + image: 'http://image.com/img.png', + stats: {}, + }) + }) +})