Skip to content

Commit

Permalink
WIP job
Browse files Browse the repository at this point in the history
  • Loading branch information
La-toile-cosmique committed Sep 19, 2024
1 parent 1abeec9 commit 522c692
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 deletions.
1 change: 1 addition & 0 deletions api/Procfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ postdeploy: npm run postdeploy
# for more information
web: exec node index.js
worker: exec node worker.js
fastworker: exec node worker.js fast
2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@
"start": "node index.js",
"start:watch": "npm run dev",
"start:job": "node worker.js",
"start:job:fast": "node worker.js fast",
"start:job:watch": "nodemon worker.js",
"start:job:fast:watch": "nodemon worker.js fast",
"test": "NODE_ENV=test npm run db:prepare && npm run test:api",
"test:api": "for testType in 'unit' 'integration' 'acceptance'; do npm run test:api:$testType || status=1 ; done ; exit $status",
"test:api:path": "NODE_ENV=test mocha --exit --recursive --reporter=${MOCHA_REPORTER:-dot}",
Expand Down
22 changes: 11 additions & 11 deletions api/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ REDIS_URL=redis://localhost:6379

# Environment
#
# Developement: will persist data in DATABASE_URL, allocate 1 to 4 connexion in pool
# Test: will persist data in TEST_DATABASE_URL, allocate 1 to 4 connexion in pool
# Production: will persist data in DATABASE_URL, allocate 1 to DATABASE_CONNECTION_POOL_MAX_SIZE connexion in pool
# Developement: will persist userId in DATABASE_URL, allocate 1 to 4 connexion in pool
# Test: will persist userId in TEST_DATABASE_URL, allocate 1 to 4 connexion in pool
# Production: will persist userId in DATABASE_URL, allocate 1 to DATABASE_CONNECTION_POOL_MAX_SIZE connexion in pool
#
# presence: optional
# type: string (any of 'development', 'test', 'production' )
# default: development in npm start task, test in npm test task
# NODE_ENV=development

# URL of the PostgreSQL database used for storing users data (filled-in or
# URL of the PostgreSQL database used for storing users userId (filled-in or
# generated).
#
# If not present, the application will crash during API boostrap.
Expand Down Expand Up @@ -324,7 +324,7 @@ MAILING_PROVIDER=mailpit
# API key provided by learning content management system.
#
# If not present and if the Redis cache were not enabled/preloaded, the
# application will crash during data fetching.
# application will crash during userId fetching.
#
# presence: required
# type: String
Expand All @@ -334,7 +334,7 @@ LCMS_API_KEY=e5d7b101-d0bd-4a3b-86c9-61edd5d39e8d
# Learning content API URL.
#
# If not present and if the Redis cache were not enabled/preloaded, the
# application will crash during data fetching.
# application will crash during userId fetching.
#
# presence: required
# type: String
Expand Down Expand Up @@ -502,28 +502,28 @@ AUTH_SECRET=the-password-must-be-at-least-32-characters-long
# ===================

# Client ID
# Refer to https://pole-emploi.io/data/api/pole-emploi-connect
# Refer to https://pole-emploi.io/userId/api/pole-emploi-connect
#
# presence: required for POLE EMPLOI authentication, optional otherwise
# type: string
# sample: POLE_EMPLOI_CLIENT_ID=

# Client secret
# Refer to https://pole-emploi.io/data/api/pole-emploi-connect
# Refer to https://pole-emploi.io/userId/api/pole-emploi-connect
#
# presence: required for POLE EMPLOI authentication, optional otherwise
# type: string
# sample: POLE_EMPLOI_CLIENT_SECRET=

# Token URL
# Refer to https://pole-emploi.io/data/api/pole-emploi-connect
# Refer to https://pole-emploi.io/userId/api/pole-emploi-connect
#
# presence: required for POLE EMPLOI authentication, optional otherwise
# type: URL
# sample: POLE_EMPLOI_TOKEN_URL=

# Test result URL
# Refer to https://pole-emploi.io/data/api/pole-emploi-connect
# Refer to https://pole-emploi.io/userId/api/pole-emploi-connect
#
# presence: required for POLE EMPLOI sending results, optional otherwise
# type: URL
Expand Down Expand Up @@ -932,7 +932,7 @@ TEST_REDIS_URL=redis://localhost:6379
# DATA PROTECTION POLICY CONFIGURATION
# ========

# Date of last data protection policy has been updated
# Date of last userId protection policy has been updated
#
# presence: required for displaying a banner on Pix App so user can see new protection policy
# type: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AnswerJob } from '../../../quest/domain/models/AnwserJob.js';
import { JobRepository } from '../../../shared/infrastructure/repositories/jobs/job-repository.js';

class AnswerJobRepository extends JobRepository {
constructor() {
super({
name: AnswerJob.name,
});
}
}

export const answerJobRepository = new AnswerJobRepository();
15 changes: 15 additions & 0 deletions api/src/quest/application/jobs/answer-job-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { JobController, JobGroup } from '../../../shared/application/jobs/job-controller.js';
import { AnswerJob } from '../../domain/models/AnwserJob.js';
import { usecases } from '../../domain/usecases/index.js';

export class AnswerJobController extends JobController {
constructor() {
super(AnswerJob.name, { jobGroup: JobGroup.FAST });
}

async handle({ data }) {
const { userId } = data;

return usecases.rewardUser({ userId });
}
}
1 change: 1 addition & 0 deletions api/src/shared/application/jobs/job-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { EntityValidationError } from '../../domain/errors.js';

export const JobGroup = {
DEFAULT: 'default',
FAST: 'fast',
};

export class JobController {
Expand Down
21 changes: 21 additions & 0 deletions api/tests/quest/unit/application/answer-job-controller_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AnswerJobController } from '../../../../src/quest/application/jobs/answer-job-controller.js';
import { AnswerJob } from '../../../../src/quest/domain/models/AnwserJob.js';
import { usecases } from '../../../../src/quest/domain/usecases/index.js';
import { JobGroup } from '../../../../src/shared/application/jobs/job-controller.js';
import { expect, sinon } from '../../../test-helper.js';

describe('Unit | Application | Jobs | AnswerJobController', function () {
it('setup the job controller configuration', async function () {
const jobController = new AnswerJobController();
expect(jobController.jobName).to.equal(AnswerJob.name);
expect(jobController.jobGroup).to.equal(JobGroup.FAST);
});

it('triggers rewarding', async function () {
sinon.stub(usecases, 'rewardUser').resolves();
const userId = Symbol('data');
const jobController = new AnswerJobController();
await jobController.handle({ data: userId });
expect(usecases.rewardUser).to.have.been.calledWith({ userId });
});
});

0 comments on commit 522c692

Please sign in to comment.