Skip to content

Commit

Permalink
Merge branch 'development' into feature/add-captcha-to-resource-form-…
Browse files Browse the repository at this point in the history
…if-set-to-everyone
  • Loading branch information
LorenzoJokhan authored Oct 6, 2023
2 parents 75bfee8 + a15fb3e commit 402555c
Show file tree
Hide file tree
Showing 37 changed files with 1,528 additions and 674 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"apostrophe-workflow": "2.40.0",
"deepl-node": "^1.7.1",
"dotenv": "^8.2.0",
"file-type": "^16.5.4",
"memory-cache": "^0.2.0",
"merge": "^2.1.1",
"morgan": "^1.10.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = [
{
name: 'themes-areas',
label: 'Themes, areas & idea types',
fields: ['themes', 'areas', 'ideaTypes']
fields: ['themes', 'areas', 'ideaTypes', 'templateDoc']
},

// this is a bit of a hack. We hide the section link with CSS
Expand Down
18 changes: 18 additions & 0 deletions packages/cms/lib/modules/openstad-global/lib/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ module.exports = [
label: 'Url van de bewerk pagina',
required: false
},
{
name: 'templateDoc',
type: 'attachment',
permission: 'admin',
svgImages: true,
label: 'Upload hier een template begroting',
// apiSyncField: 'styling.logo',
trash: true
},
{
name: 'formattedTemplateDoc',
permission: 'admin',
type: 'string',
label: 'formattedTemplateDoc',
formatField: function (value, apos, doc, req) {
return apos.attachments.url(doc.templateDoc);
},
},
{
name: 'modbreakAuthor',
type: 'string',
Expand Down
12 changes: 10 additions & 2 deletions packages/cms/lib/modules/resource-form-widgets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
extend: 'map-widgets',
label: 'Resource form',
addFields: fields,
playerData: ['siteUrl'],
beforeConstruct: function (self, options) {

if (options.resources) {
Expand Down Expand Up @@ -105,7 +106,7 @@ module.exports = {
{
name: 'budget',
label: 'Budget',
fields: ['displayBudget']
fields: ['displayBudget', 'titleBudget', 'infoBudget', 'requireBudget']
},
{
name: 'confirmation',
Expand All @@ -121,6 +122,7 @@ module.exports = {
require('./lib/helpers.js')(self, options);
require('./lib/api.js')(self, options);
require('./lib/submit.js')(self, options);
require('./lib/budget.js')(self, options);

self.on('apostrophe-docs:afterSave', 'syncConfirmationFields');
self.on('apostrophe-docs:afterTrash', 'deleteConfirmationFields');
Expand Down Expand Up @@ -178,6 +180,13 @@ module.exports = {

// Todo: refactor this to get resourceId in a different way
const activeResource = req.data.activeResource;
if(activeResource && activeResource.extraData.budgetDocuments) {
try {
activeResource.extraData.budgetDocuments = JSON.parse(activeResource.extraData.budgetDocuments);
} catch (error) {

}
}
const resources = activeResource ? [activeResource] : [];
const googleMapsApiKey = self.apos.settings.getOption(req, 'googleMapsApiKey');

Expand Down Expand Up @@ -239,7 +248,6 @@ module.exports = {
self.pushAsset('stylesheet', 'trix', {when: 'always'});
self.pushAsset('stylesheet', 'form', {when: 'always'});
self.pushAsset('stylesheet', 'main', {when: 'always'});
self.pushAsset('script', 'map', {when: 'always'});
self.pushAsset('script', 'editor', {when: 'always'});


Expand Down
90 changes: 90 additions & 0 deletions packages/cms/lib/modules/resource-form-widgets/lib/budget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const eventEmitter = require('../../../../events').emitter;
const fetch = require('node-fetch');
const fs = require('fs');

module.exports = async function(self, options) {

self.route('put', 'budget', async function(req, res) {
res.setHeader('Content-Type', 'application/json');
eventEmitter.emit('resourceCrud');
const apiUrl = self.apos.settings.getOption(req, 'apiUrl');
const siteUrl = self.apos.settings.getOption(req, 'siteUrl');
const siteId = req.data.global.siteId;

const data = req.body;
const getUrl = `${apiUrl}/api/site/${siteId}/idea/${data.id}`;
const postUrl = `${apiUrl}/api/site/${siteId}/idea/${data.id}`;

/**
* Format header
*/
const httpHeaders = {
'Accept': 'application/json',
'Content-Type': 'application/json',
};

if (req.session.jwt) {
httpHeaders['X-Authorization'] = `Bearer ${req.session.jwt}`;
}

const response = await fetch(getUrl, {
headers: httpHeaders
});

if(response.ok) {
const idea = await response.json();
let ideaBudgets = [];

try {
ideaBudgets = JSON.parse(idea?.extraData?.budgetDocuments);
}
catch(e) {

}

// d.name is a string while d.date gets returned as a string, using weak comparison
const indexToDelete = ideaBudgets.findIndex(d =>{
return d.name === data.name && d.date == data.date});

if(indexToDelete > -1) {
const removedBudget = ideaBudgets.splice(indexToDelete, 1)[0];
const hashName = removedBudget.url.substring(removedBudget.url.lastIndexOf("/") + 1);

const attachmentsPath =
`public/uploads/attachments/resource-form-uploads/${
data.id}/${hashName}`

try {
if (fs.existsSync(attachmentsPath)) {
console.log("Removing the file");
fs.rmSync(attachmentsPath, { recursive: true });
}
}
catch(e) {
return res.status(500).send(
JSON.stringify({
msg: "Het bestand kon niet verwijderd worden, probeer het nog eens.",
})
);
}
}

const putResponse = await fetch(postUrl, {
method: 'PUT',
headers: httpHeaders,
body: JSON.stringify({
extraData: {
budgetDocuments: JSON.stringify(ideaBudgets)
},
}),
});

if(putResponse.ok) {
const ideaResponse = await putResponse.json();
res.end(JSON.stringify({
id: ideaResponse.id
}));
}
}
});
}
42 changes: 40 additions & 2 deletions packages/cms/lib/modules/resource-form-widgets/lib/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ const fields = [
},
{
value: 'budget',
label: "Budget"
label: "Budget",
showFields: ['fieldRequired']
},
{
value: 'documentuploader',
label: "Document uploader"
},
{
value: 'description',
Expand Down Expand Up @@ -465,6 +470,7 @@ const fields = [
},
]
},

{
name: 'requiredDescription',
type: 'boolean',
Expand All @@ -491,6 +497,38 @@ const fields = [
label: 'Info for Images',
textarea: true
},
{
name: 'titleBudget',
type: 'string',
label: 'Alternative title for Budget',
def: 'Budget',
default: 'Budget',
placeholder: 'Budget',
},
{
name: 'infoBudget',
type: 'string',
label: 'Instruction text beneath the budget title',
def: "Enkel zichtbaar voor beheerders: vul hier het budget in euro's in voor de begrootfase. Rond het bedrag af en gebruik enkel cijfers.",
default: "Enkel zichtbaar voor beheerders: vul hier het budget in euro's in voor de begrootfase. Rond het bedrag af en gebruik enkel cijfers.",
placeholder: "Enkel zichtbaar voor beheerders: vul hier het budget in euro's in voor de begrootfase. Rond het bedrag af en gebruik enkel cijfers.",
},
{
name: 'requireBudget',
type: 'boolean',
label: 'This field is required',
choices: [
{
value: true,
label: "Yes"
},
{
value: false,
label: "No"
},
],
def: false,
},
{
name: 'uploadMultiple',
type: 'boolean',
Expand Down Expand Up @@ -822,7 +860,7 @@ const fields = [
{
name: 'displayBudget',
type: 'boolean',
label: 'Display budget for moderators?',
label: "Display budget?",
def: false
},
{
Expand Down
Loading

1 comment on commit 402555c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Published new image: openstad/frontend:feature-add-captcha-to-resource-form-if-set-to-everyone-402555c

Please sign in to comment.