Skip to content

Commit

Permalink
fix: add smtp from
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Bouquillon committed Nov 24, 2023
1 parent 52cf584 commit 4c0f339
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 66 deletions.
142 changes: 80 additions & 62 deletions back-strapi/api/box/services/box.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
"use strict";

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

const ALERTS_EMAILING = {
to: strapi.config.get('server.emails.admin'),
'500' : {
to: strapi.config.get("server.emails.admin"),
500: {
subject: '[ALERTE] Seuil des 500 atteint pour "%s"',
text: 'La box "%s" a atteint le seuil des 500, pensez à réapprovisionner',
html: `
Expand All @@ -16,7 +16,7 @@ const ALERTS_EMAILING = {
<p>Bien cordialement,<br>Le back-office Tumeplay.</p>
`,
},
'100': {
100: {
subject: '[ALERTE] Seuil des 100 atteint pour "%s"',
text: 'La box "%s" a atteint le seuil des 100, pensez à réapprovisionner',
html: `
Expand All @@ -25,7 +25,7 @@ const ALERTS_EMAILING = {
<p>Bien cordialement,<br>Le back-office Tumeplay.</p>
`,
},
'50': {
50: {
subject: '[ALERTE] Seuil des 50 atteint pour "%s"',
text: 'La box "%s" a atteint le seuil des 50, pensez à réapprovisionner',
html: `
Expand All @@ -34,7 +34,7 @@ const ALERTS_EMAILING = {
<p>Bien cordialement,<br>Le back-office Tumeplay.</p>
`,
},
'10': {
10: {
subject: '[ALERTE] Seuil des 10 atteint pour "%s"',
text: 'La box "%s" a atteint le seuil des 10, pensez à réapprovisionner',
html: `
Expand All @@ -43,91 +43,109 @@ const ALERTS_EMAILING = {
<p>Bien cordialement,<br>Le back-office Tumeplay.</p>
`,
},
'0': {
0: {
subject: '[ALERTE CRITIQUE] Plus de stock pour "%s"',
text: 'La box "%s" n\'est plus disponible, pensez à réapprovisionner.',
html: `
<p>Cher administrateur,</p>
<p>La box <b>"%s"</b> n\'est plus disponible, pensez à réapprovisionner.</p>
<p>Bien cordialement,<br>Le back-office Tumeplay.</p>
`,
}
}
},
};

function parse(str) {
var args = [].slice.call(arguments, 1),
i = 0;
i = 0;

return str.replace(/%s/g, () => args[i++]);
}

module.exports = {
module.exports = {
async decrement(box_id, quantity) {
await strapi.query('box').model.query((qb) => {
qb.where('id', box_id);
qb.increment('stock', - quantity);
}).fetch();
await strapi
.query("box")
.model.query((qb) => {
qb.where("id", box_id);
qb.increment("stock", -quantity);
})
.fetch();
},
async checkBoxAvailability(box_id) {
const box = await strapi.services.box.findOne({ id: box_id })
const box = await strapi.services.box.findOne({ id: box_id });

if (box && box.stock == 501) {
await strapi.plugins['email'].services.email.send(
Object.assign({
subject: parse(ALERTS_EMAILING['500'].subject, box.title),
text: parse(ALERTS_EMAILING['500'].text, box.title),
html: parse(ALERTS_EMAILING['500'].html, box.title),
},
{
to: ALERTS_EMAILING.to
})
await strapi.plugins["email"].services.email.send(
Object.assign(
{
subject: parse(ALERTS_EMAILING["500"].subject, box.title),
text: parse(ALERTS_EMAILING["500"].text, box.title),
html: parse(ALERTS_EMAILING["500"].html, box.title),
},
{
to: ALERTS_EMAILING.to,
from: "Tumeplay <[email protected]>",
}
)
);
} else if (box && box.stock == 101) {
await strapi.plugins['email'].services.email.send(
Object.assign({
subject: parse(ALERTS_EMAILING['100'].subject, box.title),
text: parse(ALERTS_EMAILING['100'].text, box.title),
html: parse(ALERTS_EMAILING['100'].html, box.title),
},
{
to: ALERTS_EMAILING.to
})
await strapi.plugins["email"].services.email.send(
Object.assign(
{
subject: parse(ALERTS_EMAILING["100"].subject, box.title),
text: parse(ALERTS_EMAILING["100"].text, box.title),
html: parse(ALERTS_EMAILING["100"].html, box.title),
},
{
to: ALERTS_EMAILING.to,
from: "Tumeplay <[email protected]>",
}
)
);
} else if (box && box.stock == 51) {
await strapi.plugins['email'].services.email.send(
Object.assign({
subject: parse(ALERTS_EMAILING['50'].subject, box.title),
text: parse(ALERTS_EMAILING['50'].text, box.title),
html: parse(ALERTS_EMAILING['50'].html, box.title),
},
{
to: ALERTS_EMAILING.to
})
await strapi.plugins["email"].services.email.send(
Object.assign(
{
subject: parse(ALERTS_EMAILING["50"].subject, box.title),
text: parse(ALERTS_EMAILING["50"].text, box.title),
html: parse(ALERTS_EMAILING["50"].html, box.title),
},
{
to: ALERTS_EMAILING.to,
from: "Tumeplay <[email protected]>",
}
)
);
} else if (box && box.stock == 11) {
await strapi.plugins['email'].services.email.send(
Object.assign({
subject: parse(ALERTS_EMAILING['10'].subject, box.title),
text: parse(ALERTS_EMAILING['10'].text, box.title),
html: parse(ALERTS_EMAILING['10'].html, box.title),
},
{
to: ALERTS_EMAILING.to
})
await strapi.plugins["email"].services.email.send(
Object.assign(
{
subject: parse(ALERTS_EMAILING["10"].subject, box.title),
text: parse(ALERTS_EMAILING["10"].text, box.title),
html: parse(ALERTS_EMAILING["10"].html, box.title),
},
{
to: ALERTS_EMAILING.to,
from: "Tumeplay <[email protected]>",
}
)
);
} else if (box && box.stock == 1) {
await strapi.plugins['email'].services.email.send(
Object.assign({
subject: parse(ALERTS_EMAILING['0'].subject, box.title),
text: parse(ALERTS_EMAILING['0'].text, box.title),
html: parse(ALERTS_EMAILING['0'].html, box.title),
},
{
to: ALERTS_EMAILING.to
})
await strapi.plugins["email"].services.email.send(
Object.assign(
{
subject: parse(ALERTS_EMAILING["0"].subject, box.title),
text: parse(ALERTS_EMAILING["0"].text, box.title),
html: parse(ALERTS_EMAILING["0"].html, box.title),
},
{
to: ALERTS_EMAILING.to,
from: "Tumeplay <[email protected]>",
}
)
);
}

return box && box.stock > 0;
},
};
};
14 changes: 10 additions & 4 deletions back-strapi/api/commande/controllers/commande.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ module.exports = {
await strapi.plugins["email"].services.email.sendTemplatedEmail(
{
to: entity.email,
from: "Tumeplay <[email protected]>",
},
EMAIL_ORDER_CONFIRM,
{
Expand All @@ -380,7 +381,7 @@ module.exports = {
),
}
);
} catch(e) {
} catch (e) {
console.error("EMAIL ERR", e);
}
}
Expand Down Expand Up @@ -423,9 +424,12 @@ module.exports = {

if (!referent.openingHours) referent.openingHours = {};
try {
const sendResult = await strapi.plugins["email"].services.email.sendTemplatedEmail(
const sendResult = await strapi.plugins[
"email"
].services.email.sendTemplatedEmail(
{
to: entity.email,
from: "Tumeplay <[email protected]>",
},
EMAIL_ORDER_CONFIRM_REF,
{
Expand All @@ -449,8 +453,8 @@ module.exports = {
}
);
console.log(sendResult);
console.log(JSON.stringify(sendResult,null,2));
} catch(e) {
console.log(JSON.stringify(sendResult, null, 2));
} catch (e) {
console.error("Email referent error", e);
}
}
Expand All @@ -464,6 +468,7 @@ module.exports = {
await strapi.plugins["email"].services.email.sendTemplatedEmail(
{
to: users_email.join(","),
from: "Tumeplay <[email protected]>",
},
REFERENT_ORDER_CONFIRM,
{
Expand Down Expand Up @@ -605,6 +610,7 @@ module.exports = {
await strapi.plugins["email"].services.email.sendTemplatedEmail(
{
to: entity.email,
from: "Tumeplay <[email protected]>",
},
ORDER_REFERENT_NOTIFY_USER,
{
Expand Down
2 changes: 2 additions & 0 deletions back-strapi/config/functions/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ module.exports = {

await strapi.plugins["email"].services.email.sendTemplatedEmail(
{
from: "Tumeplay <[email protected]>",
to: strapi.config.get("server.emails.cat"),
cc: strapi.config.get("server.emails.admin"),
attachments: attachments,
Expand Down Expand Up @@ -375,6 +376,7 @@ module.exports = {
strapi.plugins["email"].services.email.sendTemplatedEmail(
{
to: user.email,
from: "Tumeplay <[email protected]>",
},
CAT_TEMPLATE
);
Expand Down

0 comments on commit 4c0f339

Please sign in to comment.