Skip to content

Commit

Permalink
[TECH] Refacto - méthodes dans OrganizationPlacesLotRepository (PIX-1…
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Jan 13, 2025
2 parents ef895fe + 6e1714b commit e76408f
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const findOrganizationPlacesLot = async function ({ organizationId, organizationPlacesLotRepository }) {
return organizationPlacesLotRepository.findByOrganizationId(organizationId);
return organizationPlacesLotRepository.findByOrganizationIdWithJoinedUsers(organizationId);
};

export { findOrganizationPlacesLot };
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const getDataOrganizationsPlacesStatistics = withTransaction(async function ({

const organizationWithPlacesIds = organizationWithPlaces.map((organization) => organization.id);

const placesLots = await organizationPlacesLotRepository.findAllByOrganizationIds(organizationWithPlacesIds);
const placesLots = await organizationPlacesLotRepository.findAllByOrganizationIds({
organizationIds: organizationWithPlacesIds,
});

const placeRepartitions =
await organizationLearnerRepository.findAllLearnerWithAtLeastOneParticipationByOrganizationIds(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@

/**
* @typedef {object} organizationPlacesLotRepository
* @property {function} findAllByOrganizationId
* @property {function} findAllByOrganizationIds
*/

const getOrganizationPlacesLots = async function ({ organizationId, organizationPlacesLotRepository }) {
return await organizationPlacesLotRepository.findAllNotDeletedByOrganizationId(organizationId);
if (!organizationId) {
throw new Error('You must provide at least one organizationId.');
}

return organizationPlacesLotRepository.findAllByOrganizationIds({
organizationIds: [organizationId],
callOrderByAndRemoveDeleted: true,
});
};

export { getOrganizationPlacesLots };
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ const getOrganizationPlacesStatistics = async function ({
organizationPlacesLotRepository,
organizationLearnerRepository,
}) {
const placesLots = await organizationPlacesLotRepository.findAllByOrganizationId(organizationId);
if (!organizationId) {
throw new Error('You must provide at least one organizationId.');
}

const placesLots = await organizationPlacesLotRepository.findAllByOrganizationIds({
organizationIds: [organizationId],
});

const placeRepartition =
await organizationLearnerRepository.findAllLearnerWithAtLeastOneParticipationByOrganizationId(organizationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,61 @@ import { DeletedError, NotFoundError } from '../../../../shared/domain/errors.js
import { OrganizationPlacesLotManagement } from '../../domain/read-models/OrganizationPlacesLotManagement.js';
import { PlacesLot } from '../../domain/read-models/PlacesLot.js';

const findByOrganizationId = async function (organizationId) {
const results = await knex('organization-places')
.select(
'organization-places.id AS id',
'count',
'activationDate',
'expirationDate',
'reference',
'category',
'users.firstName AS creatorFirstName',
'users.lastName AS creatorLastName',
)
.join('users', 'users.id', 'createdBy')
.where({ organizationId })
.whereNull('deletedAt')
.orderBy('activationDate', 'desc')
.orderBy('expirationDate', 'desc')
.orderBy('organization-places.createdAt', 'desc');
const findByOrganizationIdWithJoinedUsers = async (organizationId) => {
const extraColumns = [
'users.firstName AS creatorFirstName',
'users.lastName AS creatorLastName',
'organization-places.reference',
'organization-places.category',
];

const results = await baseQuery({ organizationIds: [organizationId], callOrderByAndRemoveDeleted: true })
.select(...extraColumns)
.join('users', 'users.id', 'organization-places.createdBy');

return results.map((result) => {
return new OrganizationPlacesLotManagement(result);
});
};

//On utilise pas le findByOrganizationId car c'est un aggregat avec la table users, et les regles métiers ne sont pas utiles ici
const findAllByOrganizationId = async function (organizationId) {
const baseQuery = ({ organizationIds, callOrderByAndRemoveDeleted = false }) => {
const knexConn = DomainTransaction.getConnection();
const placesLots = await knexConn('organization-places')
.select('id', 'count', 'activationDate', 'expirationDate', 'deletedAt')
.where({ organizationId });
return placesLots.map((e) => new PlacesLot(e));
};

const findAllByOrganizationIds = async function (organizationIds) {
const knexConn = DomainTransaction.getConnection();
const placesLots = await knexConn('organization-places')
.select('id', 'count', 'organizationId', 'activationDate', 'expirationDate', 'deletedAt')
.whereIn('organizationId', organizationIds);
let query = knexConn('organization-places')
.select(
'organization-places.id',
'organization-places.count',
'organization-places.organizationId',
'organization-places.activationDate',
'organization-places.expirationDate',
'organization-places.deletedAt',
)
.whereIn('organization-places.organizationId', organizationIds);

return placesLots.map((e) => new PlacesLot(e));
if (callOrderByAndRemoveDeleted) {
query = orderByAndRemoveDeleted(query);
}

return query;
};

//On utilise pas le findByOrganizationId car c'est un aggregat avec la table users, et les regles métiers ne sont pas utiles ici
//On utilise pas le findAllByOrganizationId car on a a pas besoin des deleted et nous avons besoin de l'ordonnance
const findAllNotDeletedByOrganizationId = async function (organizationId) {
const knexConn = DomainTransaction.getConnection();
const placesLots = await knexConn('organization-places')
.select('id', 'count', 'activationDate', 'expirationDate', 'deletedAt')
.where({ organizationId })
.whereNull('deletedAt')
const findAllByOrganizationIds = async ({ organizationIds, callOrderByAndRemoveDeleted = false }) => {
const placesLots = await baseQuery({ organizationIds, callOrderByAndRemoveDeleted });
return placesLots.map((placesLot) => new PlacesLot(placesLot));
};

const orderByAndRemoveDeleted = (query) => {
return query
.whereNull('organization-places.deletedAt')
.orderBy(
knex.raw(
'CASE WHEN "activationDate" <= now() AND "expirationDate" >= now() THEN 1 WHEN "activationDate" > now() THEN 2 ELSE 3 END',
'CASE WHEN "organization-places"."activationDate" <= now() AND "organization-places"."expirationDate" >= now() THEN 1 WHEN "organization-places"."activationDate" > now() THEN 2 ELSE 3 END',
),
'asc',
)
.orderBy('expirationDate', 'desc')
.orderBy('activationDate', 'desc')
.orderBy('createdAt', 'desc');

return placesLots.map((placesLot) => new PlacesLot(placesLot));
.orderBy('organization-places.expirationDate', 'desc')
.orderBy('organization-places.activationDate', 'desc')
.orderBy('organization-places.createdAt', 'desc');
};

const get = async function (id) {
Expand Down Expand Up @@ -105,12 +99,4 @@ const remove = async function ({ id, deletedBy }) {
}
};

export {
create,
findAllByOrganizationId,
findAllByOrganizationIds,
findAllNotDeletedByOrganizationId,
findByOrganizationId,
get,
remove,
};
export { create, findAllByOrganizationIds, findByOrganizationIdWithJoinedUsers, get, remove };
Loading

0 comments on commit e76408f

Please sign in to comment.