Skip to content

Commit

Permalink
tech: dynamic banners poc
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuGilet committed Jan 10, 2025
1 parent b71a251 commit a8ac30c
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { targetProfileRoutes } from './src/prescription/target-profile/routes.js
import { profileRoutes } from './src/profile/routes.js';
import { questRoutes } from './src/quest/routes.js';
import { schoolRoutes } from './src/school/routes.js';
import { bannerRoutes } from './src/banner/routes.js';
import { config } from './src/shared/config.js';
import { monitoringTools } from './src/shared/infrastructure/monitoring-tools.js';
import { plugins } from './src/shared/infrastructure/plugins/index.js';
Expand Down Expand Up @@ -243,6 +244,7 @@ const setupRoutesAndPlugins = async function (server) {
...certificationRoutes,
...prescriptionRoutes,
...parcoursupRoutes,
bannerRoutes,
{
name: 'root',
register: async function (server) {
Expand Down
38 changes: 38 additions & 0 deletions api/src/banner/application/banner-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Serializer } from 'jsonapi-serializer';

import { config } from '../../shared/config.js';
import { RedisClient } from '../../shared/infrastructure/utils/RedisClient.js';

const getInformationBanner = async function (request) {
const target = request.path.target;

const redisClient = new RedisClient(config.temporaryStorage.redisUrl, 'infrastructure');
const rawBanners = await redisClient.get(`information-banner:${target}`);
const banners = JSON.parse(rawBanners);

banners?.forEach((banner, index) => {
banner.id = index + 1;
});

const informationBanners = {
id: target,
banners,
};

await redisClient.quit();

return new Serializer('information-banners', {
attributes: ['banners'],
banners: {
included: true,
ref: 'id',
attributes: ['severity', 'message'],
},
}).serialize(informationBanners);
};

const bannerController = {
getInformationBanner,
};

export { bannerController };
17 changes: 17 additions & 0 deletions api/src/banner/application/banner-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { bannerController } from './banner-controller.js';

const register = async function (server) {
server.route([
{
method: 'GET',
path: '/api/information-banners/{target}',
config: {
auth: false,
handler: bannerController.getInformationBanner,
},
},
]);
};

const name = 'src-banners-api';
export { name, register };
3 changes: 3 additions & 0 deletions api/src/banner/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as bannerRoute from './application/banner-route.js';

export const bannerRoutes = [bannerRoute];
1 change: 1 addition & 0 deletions api/src/monitoring/infrastructure/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ export class Metrics {
this.#addMetricPointWithoutRegistration(zero);
});
await metrics.flush();
console.log('FLUSH DONE !');
}
}
6 changes: 6 additions & 0 deletions certif/app/models/banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Model, { attr } from '@ember-data/model';

export default class InformationBanner extends Model {
@attr() severity;
@attr() message;
}
5 changes: 5 additions & 0 deletions certif/app/models/information-banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Model, { hasMany } from '@ember-data/model';

export default class InformationBanner extends Model {
@hasMany('banners', { async: false, inverse: null }) banners;
}
39 changes: 36 additions & 3 deletions certif/app/routes/application.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { action } from '@ember/object';
import Route from '@ember/routing/route';
import { service } from '@ember/service';
import ENV from 'pix-certif/config/environment';
import RSVP from 'rsvp';

export default class ApplicationRoute extends Route {
@service featureToggles;
@service currentDomain;
@service currentUser;
@service session;
@service store;

async beforeModel(transition) {
await this.session.setup();
Expand All @@ -17,10 +21,39 @@ export default class ApplicationRoute extends Route {
await this.session.handleLocale({ isFranceDomain, localeFromQueryParam, userLocale });
}

model() {
return {
async model() {
const informationBanner = this.store.findRecord('information-banner', `${ENV.APP.APPLICATION_NAME}`);
return RSVP.hash({
title: this.currentDomain.isFranceDomain ? 'Pix Certif (France)' : 'Pix Certif (hors France)',
headElement: document.querySelector('head'),
};
informationBanner,
});
}

afterModel() {
this.poller = setInterval(async () => {
try {
this.store.findRecord('information-banner', `${ENV.APP.APPLICATION_NAME}`);
} catch (response) {
this.#stopPolling();
}
}, 2000);
}

deactivate() {
this.#stopPolling();
}

@action
error() {
this.#stopPolling();
return true;
}

#stopPolling() {
if (this.poller) {
clearInterval(this.poller);
this.poller = null;
}
}
}
6 changes: 6 additions & 0 deletions certif/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

<CommunicationBanner />

{{#each @model.informationBanner.banners as |banner|}}
<PixBannerAlert @type={{banner.severity}} @canCloseBanner={{false}}>
{{text-with-multiple-lang banner.message}}
</PixBannerAlert>
{{/each}}

{{outlet}}

<PixToastContainer @closeButtonAriaLabel={{t "common.notifications.close-button.extra-information"}} />
1 change: 1 addition & 0 deletions certif/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = function (environment) {

APP: {
API_HOST: process.env.API_HOST || '',
APPLICATION_NAME: process.env.APP || 'pix-certif-local',
BANNER: {
CONTENT: process.env.BANNER_CONTENT || '',
TYPE: process.env.BANNER_TYPE || '',
Expand Down
4 changes: 4 additions & 0 deletions certif/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ function routes() {

_configureCertificationCenterInvitationRoutes(this);
_configureCertificationCenterMemberRoutes(this);

this.get('/information-banners', (schema) => {
return schema.informationBanners.find('pix-certif-test ');
});
}

function _configureCertificationCenterInvitationRoutes(context) {
Expand Down
7 changes: 7 additions & 0 deletions certif/mirage/factories/information-banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Factory } from 'miragejs';

export default Factory.extend({
banners() {
return [];
},
});
2 changes: 1 addition & 1 deletion certif/tests/acceptance/terms-of-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
createCertificationPointOfContactWithTermsOfServiceNotAccepted,
} from '../helpers/test-init';

module('Acceptance | terms-of-service', function (hooks) {
module.only('Acceptance | terms-of-service', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);

Expand Down

0 comments on commit a8ac30c

Please sign in to comment.