Skip to content

Commit

Permalink
Preselection flow and email
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucieo committed Mar 1, 2024
1 parent 5e4fc3e commit 8ca2f7b
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 67 deletions.
8 changes: 8 additions & 0 deletions back/api/disponibility/config/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
"config": {
"policies": ["global::is-place", "is-owner"]
}
},
{
"method": "POST",
"path": "/disponibilities/:id/campaign/:campaignId/confirm",
"handler": "disponibility.campaignConfirm",
"config": {
"policies": ["global::is-place", "is-owner"]
}
}
]
}
126 changes: 96 additions & 30 deletions back/api/disponibility/controllers/disponibility.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,145 @@
"use strict";
const isAfter = require("date-fns/isAfter");
const max = require("date-fns/max");
'use strict'
const isAfter = require('date-fns/isAfter')
const max = require('date-fns/max')
const format = require('date-fns/format')
const fr = require('date-fns/locale/fr')

const locale = {
locale: fr,
}

/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
* to customize this controller
*/

const params = [
"dispositif",
"dispositif.companies",
"dispositif.users_permissions_users",
];
'dispositif',
'dispositif.companies',
'dispositif.users_permissions_users',
]

module.exports = {
async find(ctx) {
const entities = ctx.query._q
? await strapi.services.disponibility.search(ctx.query, params)
: await strapi.services.disponibility.find(ctx.query, params);
: await strapi.services.disponibility.find(ctx.query, params)

return entities.map((entity) =>
sanitizeEntity(entity, { model: strapi.models.disponibility })
);
sanitizeEntity(entity, { model: strapi.models.disponibility }),
)
},

createMany: async (ctx) => {
const user = ctx.state.user;
const user = ctx.state.user
if (!Array.isArray(ctx.request.body))
return ctx.badRequest(
null,
formatError({
id: "error.arrayProvide",
message: "You must provide an array",
})
);
id: 'error.arrayProvide',
message: 'You must provide an array',
}),
)

const newDispo = await Promise.all(
ctx.request.body.map((dispo) =>
strapi.query("disponibility").create(dispo)
)
);
strapi.query('disponibility').create(dispo),
),
)

const maxDate = max(newDispo.map((dispo) => new Date(dispo.end)));
const place = newDispo[0].espace;
const maxDate = max(newDispo.map((dispo) => new Date(dispo.end)))
const place = newDispo[0].espace

if (!place.filledUntil || isAfter(maxDate, new Date(place.filledUntil))) {
const data = newDispo?.campaign ? {}:{ filledUntil: maxDate };
const data = newDispo?.campaign ? {} : { filledUntil: maxDate }
if (!place.filledUntil) {
data["published"] = true;
data['published'] = true
}
strapi
.query("espace")
.query('espace')
.update({ id: place.id }, data)
.then(() => {
if (!place.filledUntil) {
// Send email to administration
strapi.plugins["email"].services.email.sendEmail(
strapi.plugins['email'].services.email.sendEmail(
{
to: process.env.EMAIL_RECIPIENT,
},
{
templateId: "admin-new-place",
templateId: 'admin-new-place',
},
{
user_type: user.type,
user_name: user.firstname,
slug: place.slug,
url_strapi: `${process.env.BASE_URL}/admin/plugins/content-manager/collectionType/application::espace.espace/${place.id}`,
},
true
);
true,
)
}
});
})
}

return newDispo;
return newDispo
},
async campaignConfirm(ctx) {
const { id, campaignId } = ctx.params

const disponibility = await strapi.services.disponibility.findOne({ id }, [
'applications',
'espace.users_permissions_user',
])

if (!disponibility) {
return ctx.throw(404, 'Disponibility not found')
}

const updatedApplications = disponibility.applications
.filter((application) => {
return (
application.campaign.toString() === campaignId.toString() &&
application.status === 'preselected'
)
})
.map((application) => {
return { ...application, status: 'confirmed' }
})

await Promise.all(
updatedApplications.map(async (updatedApplication) => {
await strapi.services.application.update(
{ id: updatedApplication?.id },
updatedApplication,
)
}),
)

try {
const lieu = disponibility.espace.users_permissions_user

const campaign = await strapi.services.campaign.findOne({
id: disponibility.campaign,
})
// // Send email to the place
strapi.plugins['email'].services.email.sendEmail(
{
to: lieu.email,
},
{
templateId: 'place-preselection-confirm',
subject: `Votre pré-selection a été prise en compte`,
},
{
user_name: lieu.firstname,
campaign_name: campaign?.name,
espace_name: disponibility.espace.name,
date_start: format(new Date(disponibility.start), 'dd/MM', locale),
date_end: format(new Date(disponibility.end), 'dd/MM', locale),
},
)
} catch (e) {
console.log('error', e)
}
return { ...disponibility, applications: updatedApplications }
},
};
}
71 changes: 71 additions & 0 deletions back/api/disponibility/documentation/1.0.0/disponibility.json
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,77 @@
}
}
}
},
"/disponibilities/{id}/campaign/{campaignId}/confirm": {
"post": {
"deprecated": false,
"description": "Create a new record",
"responses": {
"200": {
"description": "response",
"content": {
"application/json": {
"schema": {
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "Not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"summary": "",
"tags": [
"Disponibility"
],
"requestBody": {
"description": "",
"required": true,
"content": {
"application/json": {
"schema": {
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
}
}
}
},
"components": {
Expand Down
Loading

0 comments on commit 8ca2f7b

Please sign in to comment.