Skip to content

Commit

Permalink
add platform parameter to announcements endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Oct 19, 2023
1 parent 8ce588e commit 4949956
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/controllers/v1/announcements_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Site = require( "../../models/site" );
const AnnouncementsController = class AnnouncementsController {
static async search( req ) {
let query = squel.select( )
.field( "announcements.id, body, placement, dismissible, locales, start, \"end\"" )
.field( "announcements.id, body, placement, dismissible, locales, platforms, start, \"end\"" )
.from( "announcements" )
.where( "NOW() at time zone 'utc' between start and \"end\"" )
.order( "announcements.id" );
Expand All @@ -29,6 +29,15 @@ const AnnouncementsController = class AnnouncementsController {
query = query.where( placementClause );
}

if ( req.query.platform ) {
// given a platform parameter, return only announcements that include that platform,
// or announcements with no platform specified
query = query.where( "? = ANY( platforms ) OR platforms IS NULL OR platforms = '{}'", req.query.platform );
} else {
// if there is no platform parameter, return only announcements with no platform specified
query = query.where( "platforms IS NULL OR platforms = '{}'" );
}

// site_id filter
if ( req.userSession ) {
// authenticated requests include announcements targeted at the users site,
Expand Down
6 changes: 6 additions & 0 deletions openapi/schema/request/announcements_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ module.exports = Joi.object( ).keys( {
"mobile/home",
"mobile"
),
platform: Joi.string( ).valid(
"inat-ios",
"inat-android",
"seek",
"inatrn"
),
locale: Joi.string( ),
fields: Joi.any( )
} ).unknown( false );
1 change: 1 addition & 0 deletions openapi/schema/response/announcement.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = Joi.object( ).keys( {
id: Joi.number( ).integer( ).required( ),
body: Joi.string( ),
placement: Joi.string( ),
platforms: Joi.array( ).items( Joi.string( ) ),
dismissible: Joi.boolean( ),
locales: Joi.array( ).items( Joi.string( ) ),
start: Joi.date( ),
Expand Down
3 changes: 2 additions & 1 deletion schema/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ CREATE TABLE public.announcements (
updated_at timestamp without time zone,
locales text[] DEFAULT '{}'::text[],
dismiss_user_ids integer[] DEFAULT '{}'::integer[],
dismissible boolean DEFAULT false
dismissible boolean DEFAULT false,
platforms text[] DEFAULT '{}'::text[]
);


Expand Down
24 changes: 20 additions & 4 deletions schema/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,8 @@
"updated_at": "2023-01-01 00:00:00",
"placement": "mobile/home",
"locales": "{}",
"dismissible": true
"dismissible": true,
"platforms": "{}"
},
{
"id": 2,
Expand All @@ -1862,7 +1863,8 @@
"updated_at": "2023-01-01 00:00:00",
"placement": "mobile/home",
"locales": "{}",
"dismissible": true
"dismissible": true,
"platforms": "{}"
},
{
"id": 3,
Expand All @@ -1873,7 +1875,8 @@
"updated_at": "2023-01-01 00:00:00",
"placement": "mobile/home",
"locales": "{en-US,fr}",
"dismissible": true
"dismissible": true,
"platforms": "{}"
},
{
"id": 4,
Expand All @@ -1884,7 +1887,20 @@
"updated_at": "2023-01-01 00:00:00",
"placement": "mobile/home",
"locales": "{en}",
"dismissible": true
"dismissible": true,
"platforms": "{}"
},
{
"id": 5,
"body": "Active announcement for inat-ios and inat-android",
"start": "2023-01-01 00:00:00",
"end": "2100-01-01 00:00:00",
"created_at": "2023-01-01 00:00:00",
"updated_at": "2023-01-01 00:00:00",
"placement": "mobile/home",
"locales": "{}",
"dismissible": true,
"platforms": "{inat-ios,inat-android}"
}
],
"comments": [
Expand Down
22 changes: 22 additions & 0 deletions test/integration/v2/announcements.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ describe( "Announcements", ( ) => {
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );

it( "returns announcements based on platform", function ( done ) {
const inatiOSAnnouncement = _.find(
fixtures.postgresql.announcements, a => a.platforms.match( /inat-ios/ )
);
request( this.app ).get( "/v2/announcements?fields=all&platform=inat-ios" ).expect( res => {
expect( res.body.results ).to.not.be.empty;
expect( _.every( res.body.results, r => (
r.platforms.includes( "inat-ios" ) || _.isEmpty( r.platforms )
) ) ).to.be.true;
expect( _.map( res.body.results, "id" ) ).to.include( inatiOSAnnouncement.id );
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );

it( "does not return announcements with a platform not matching parameter", function ( done ) {
request( this.app ).get( "/v2/announcements?fields=all&platform=seek" ).expect( res => {
expect( res.body.results ).to.not.be.empty;
expect( _.every( res.body.results, r => _.isEmpty( r.platforms ) ) ).to.be.true;
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );
} );

describe( "dismiss", ( ) => {
Expand Down

0 comments on commit 4949956

Please sign in to comment.