-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Ajoute une API interne pour générer un zip avec les attesta…
- Loading branch information
Showing
18 changed files
with
370 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
api/db/migrations/20241002122830_add-key-column-to-attestations-table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const TABLE_NAME = 'attestations'; | ||
const COLUMN_NAME = 'key'; | ||
|
||
const up = async function (knex) { | ||
await knex.schema.table(TABLE_NAME, function (table) { | ||
table.string(COLUMN_NAME).notNullable().unique(); | ||
}); | ||
}; | ||
|
||
const down = async function (knex) { | ||
await knex.schema.table(TABLE_NAME, function (table) { | ||
table.dropColumn(COLUMN_NAME); | ||
}); | ||
}; | ||
|
||
export { down, up }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as path from 'node:path'; | ||
import * as url from 'node:url'; | ||
|
||
import { usecases } from '../../domain/usecases/index.js'; | ||
import * as pdfWithFormSerializer from '../../infrastructure/serializers/pdf/pdf-with-form-serializer.js'; | ||
|
||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
|
||
export const generateAttestations = async function ({ | ||
attestationKey, | ||
userIds, | ||
dependencies = { pdfWithFormSerializer }, | ||
}) { | ||
const { templateName, data } = await usecases.getAttestationDataForUsers({ attestationKey, userIds }); | ||
|
||
const templatePath = path.join(__dirname, `../../infrastructure/serializers/pdf/templates/${templateName}.pdf`); | ||
|
||
return dependencies.pdfWithFormSerializer.generate(templatePath, data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const ATTESTATIONS = { | ||
SIXTH_GRADE: 'SIXTH_GRADE', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
export class ProfileReward { | ||
constructor({ id, rewardId, rewardType } = {}) { | ||
constructor({ id, rewardId, rewardType, createdAt } = {}) { | ||
this.id = id; | ||
this.rewardId = rewardId; | ||
this.rewardType = rewardType; | ||
this.createdAt = createdAt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import capitalize from 'lodash/capitalize.js'; | ||
|
||
export class User { | ||
id; | ||
#firstName; | ||
#lastName; | ||
|
||
constructor({ id, firstName, lastName }) { | ||
this.id = id; | ||
this.#firstName = firstName; | ||
this.#lastName = lastName; | ||
} | ||
|
||
get fullName() { | ||
return capitalize(this.#firstName) + ' ' + this.#lastName.toUpperCase(); | ||
} | ||
|
||
toForm(createdAt) { | ||
const map = new Map(); | ||
|
||
map.set('fullName', this.fullName); | ||
map.set('filename', this.fullName + Date.now()); | ||
map.set('date', createdAt); | ||
|
||
return map; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
api/src/profile/domain/usecases/get-attestation-data-for-users.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export async function getAttestationDataForUsers({ attestationKey, userIds, userRepository, profileRewardRepository }) { | ||
const users = await userRepository.getByIds({ userIds }); | ||
const profileRewards = await profileRewardRepository.getByAttestationKeyAndUserIds({ attestationKey, userIds }); | ||
|
||
return profileRewards.map(({ userId, createdAt }) => { | ||
const user = users.find((user) => user.id === userId); | ||
return user.toForm(createdAt); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as usersApi from '../../../../src/identity-access-management/application/api/users-api.js'; | ||
import { injectDependencies } from '../../../../src/shared/infrastructure/utils/dependency-injection.js'; | ||
import * as userRepository from './user-repository.js'; | ||
|
||
const repositoriesWithoutInjectedDependencies = { | ||
userRepository, | ||
}; | ||
|
||
const dependencies = { | ||
usersApi, | ||
}; | ||
|
||
const repositories = injectDependencies(repositoriesWithoutInjectedDependencies, dependencies); | ||
|
||
export { repositories }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
api/src/profile/infrastructure/repositories/user-repository.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { User } from '../../domain/models/User.js'; | ||
|
||
export async function getByIds({ userIds, usersApi }) { | ||
const userDTOs = await usersApi.getUserDetailsByUserIds({ userIds }); | ||
|
||
return userDTOs.map((userDTO) => new User(userDTO)); | ||
} |
Binary file added
BIN
+443 KB
...src/profile/infrastructure/serializers/pdf/templates/sixth-grade-attestation-template.pdf
Binary file not shown.
44 changes: 44 additions & 0 deletions
44
api/tests/profile/integration/domain/usecases/get-attestation-data-for-users_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { User } from '../../../../../src/profile/domain/models/User.js'; | ||
import { usecases } from '../../../../../src/profile/domain/usecases/index.js'; | ||
import { databaseBuilder, expect, sinon } from '../../../../test-helper.js'; | ||
|
||
describe('Profile | Integration | Domain | get-attestation-data-for-users', function () { | ||
let clock; | ||
const now = new Date('2022-12-25'); | ||
|
||
beforeEach(function () { | ||
clock = sinon.useFakeTimers({ | ||
now, | ||
toFake: ['Date'], | ||
}); | ||
}); | ||
|
||
afterEach(async function () { | ||
clock.restore(); | ||
}); | ||
|
||
describe('#getAttestationDataForUsers', function () { | ||
it('should return profile rewards', async function () { | ||
const attestation = databaseBuilder.factory.buildAttestation(); | ||
const firstUser = new User(databaseBuilder.factory.buildUser()); | ||
const secondUser = new User(databaseBuilder.factory.buildUser()); | ||
const firstCreatedAt = databaseBuilder.factory.buildProfileReward({ | ||
rewardId: attestation.id, | ||
userId: firstUser.id, | ||
}).createdAt; | ||
const secondCreatedAt = databaseBuilder.factory.buildProfileReward({ | ||
rewardId: attestation.id, | ||
userId: secondUser.id, | ||
}).createdAt; | ||
|
||
await databaseBuilder.commit(); | ||
|
||
const results = await usecases.getAttestationDataForUsers({ | ||
attestationKey: attestation.key, | ||
userIds: [firstUser.id, secondUser.id], | ||
}); | ||
|
||
expect(results).to.deep.equal([firstUser.toForm(firstCreatedAt), secondUser.toForm(secondCreatedAt)]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.