Skip to content

Commit

Permalink
feat(server): create events when a housing is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Falinor committed Jan 22, 2025
1 parent 01cb088 commit b27247a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
87 changes: 84 additions & 3 deletions server/src/controllers/housingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import {
Owners,
ownerTable
} from '~/repositories/ownerRepository';
import { HousingStatusApi } from '~/models/HousingStatusApi';
import { HousingStatusApi, toHousingStatus } from '~/models/HousingStatusApi';
import {
EventRecordDBO,
Events,
eventsTable,
HousingEvents,
Expand Down Expand Up @@ -68,20 +69,25 @@ import {
OCCUPANCY_VALUES
} from '@zerologementvacant/models';
import { EstablishmentApi } from '~/models/EstablishmentApi';
import { UserApi, UserRoles } from '~/models/UserApi';

describe('Housing API', () => {
const { app } = createServer();

const establishment = genEstablishmentApi();
const user = genUserApi(establishment.id);
const visitor: UserApi = {
...genUserApi(establishment.id),
role: UserRoles.Visitor
};
const anotherEstablishment = genEstablishmentApi();
const anotherUser = genUserApi(anotherEstablishment.id);

beforeAll(async () => {
await Establishments().insert(
[establishment, anotherEstablishment].map(formatEstablishmentApi)
);
await Users().insert([user, anotherUser].map(formatUserApi));
await Users().insert([user, visitor, anotherUser].map(formatUserApi));
});

describe('GET /housing/{id}', () => {
Expand Down Expand Up @@ -570,7 +576,15 @@ describe('Housing API', () => {
expect(status).toBe(constants.HTTP_STATUS_NOT_FOUND);
});

it.todo('should throw if the user is a visitor');
it('should throw if the user is a visitor', async () => {
const { status } = await request(app)
.put(testRoute(housing.id))
.send(payload)
.type('json')
.use(tokenProvider(visitor));

expect(status).toBe(constants.HTTP_STATUS_UNAUTHORIZED);
});

it('should return the housing', async () => {
const { body, status } = await request(app)
Expand Down Expand Up @@ -610,6 +624,73 @@ describe('Housing API', () => {
occupancy_intended: payload.occupancyIntended
});
});

it('should not create events if there is no change', async () => {
const payload: HousingUpdatePayloadDTO = {
status: toHousingStatus(housing.status),
subStatus: housing.subStatus,
occupancy: housing.occupancy,
occupancyIntended: housing.occupancyIntended,
precisions: housing.precisions,
vacancyReasons: housing.vacancyReasons
};

const { status } = await request(app)
.put(testRoute(housing.id))
.send(payload)
.type('json')
.use(tokenProvider(user));

expect(status).toBe(constants.HTTP_STATUS_OK);
const events = await HousingEvents().where({
housing_geo_code: housing.geoCode,
housing_id: housing.id
});
expect(events).toHaveLength(0);
});

it('should create an event related to the status change', async () => {
const { status } = await request(app)
.put(testRoute(housing.id))
.send(payload)
.type('json')
.use(tokenProvider(user));

expect(status).toBe(constants.HTTP_STATUS_OK);
const event = await Events()
.join(housingEventsTable, 'event_id', 'id')
.where({
housing_id: housing.id,
housing_geo_code: housing.geoCode,
name: 'Changement de statut de suivi'
})
.first();
expect(event).toMatchObject<Partial<EventRecordDBO<any>>>({
name: 'Changement de statut de suivi',
created_by: user.id
});
});

it('should create an event related to the occupancy change', async () => {
const { status } = await request(app)
.put(testRoute(housing.id))
.send(payload)
.use(tokenProvider(user));

expect(status).toBe(constants.HTTP_STATUS_OK);
const event = await Events()
.join(housingEventsTable, 'event_id', 'id')
.where({
housing_geo_code: housing.geoCode,
housing_id: housing.id,
name: "Modification du statut d'occupation"
})
.first();
expect(event).toMatchObject<Partial<EventRecordDBO<any>>>({
name: "Modification du statut d'occupation",
created_by: user.id
});
});
});

describe('POST /housing/list', () => {
Expand Down
28 changes: 25 additions & 3 deletions server/src/controllers/housingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ async function updateNext(
request: Request<HousingPathParams, HousingDTO, HousingUpdatePayloadDTO>,
response: Response
): Promise<void> {
const { body, establishment, params } = request as AuthenticatedRequest<
const { auth, body, establishment, params } = request as AuthenticatedRequest<
HousingPathParams,
HousingDTO,
HousingUpdatePayloadDTO
Expand All @@ -348,6 +348,22 @@ async function updateNext(
};
await startTransaction(async () => {
await housingRepository.update(updated);
await createHousingUpdateEvents(
housing,
{
statusUpdate: {
status: fromHousingStatus(body.status),
subStatus: body.subStatus,
precisions: body.precisions,
vacancyReasons: body.vacancyReasons
},
occupancyUpdate: {
occupancy: body.occupancy,
occupancyIntended: body.occupancyIntended
}
},
auth.userId
);
});

response.status(constants.HTTP_STATUS_OK).json(toHousingDTO(updated));
Expand Down Expand Up @@ -477,8 +493,14 @@ async function createHousingUpdateEvents(
statusUpdate &&
(housingApi.status !== statusUpdate.status ||
housingApi.subStatus !== statusUpdate.subStatus ||
!_.isEqual(housingApi.precisions, statusUpdate.precisions) ||
!_.isEqual(housingApi.vacancyReasons, statusUpdate.vacancyReasons))
!_.isEqual(
housingApi.precisions ?? null,
statusUpdate.precisions ?? null
) ||
!_.isEqual(
housingApi.vacancyReasons ?? null,
statusUpdate.vacancyReasons ?? null
))
) {
await eventRepository.insertHousingEvent({
id: uuidv4(),
Expand Down

0 comments on commit b27247a

Please sign in to comment.