Skip to content

Commit

Permalink
PDF Generation with for a single application
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucieo committed Mar 4, 2024
1 parent dde40a3 commit d91b19e
Show file tree
Hide file tree
Showing 12 changed files with 1,038 additions and 157 deletions.
15 changes: 11 additions & 4 deletions back/api/application/config/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"path": "/applications/me/:campaignId",
"handler": "application.myApplications",
"config": {
"policies": ["global::is-authenticated"],
"operationId": "myApplications",
"description": "Get applications related to current user"
"policies": ["global::is-authenticated"],
"operationId": "myApplications",
"description": "Get applications related to current user"
}
},
{
Expand Down Expand Up @@ -57,7 +57,14 @@
"config": {
"policies": ["is-concerned"]
}
},
{
"method": "GET",
"path": "/applications/:id/generate-pdf",
"handler": "application.generatePdf",
"config": {
"policies": ["can-update"]
}
}
]
}

44 changes: 44 additions & 0 deletions back/api/application/controllers/ApplicationPdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const React = require('react')
const {
Document,
Page,
Text,
View,
StyleSheet,
} = require('@react-pdf/renderer')

// Create styles
const styles = StyleSheet.create({
page: {
flexDirection: 'column',
backgroundColor: '#E4E4E4',
},
section: {
margin: 10,
padding: 10,
flexGrow: 1,
},
})

// Create Document Component
const ApplicationPdf = ({ applicationData }) => {
const content = React.createElement(
Document,
null,
React.createElement(
Page,
{ size: 'A4', style: styles.page },
React.createElement(
View,
{ style: styles.section },
React.createElement(Text, { color: 'red' }, `Name: BBBBBBBBBB`),
React.createElement(Text, { color: 'red' }, `Email:AAAAAAA`),
// Add more fields as needed
),
),
)
console.log(content)
return content
}

module.exports = ApplicationPdf
65 changes: 49 additions & 16 deletions back/api/application/controllers/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict';
'use strict'
const ApplicationPdf = require('./ApplicationPdf')
const ReactPDF = require('@react-pdf/renderer')
const React = require('react')

/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
Expand All @@ -7,18 +10,28 @@

module.exports = {
async myApplications(ctx) {
const populateCommon = ['disponibility.espace', 'disponibility.espace.users_permissions_user', 'creation_file' ]
const { id, type } = ctx.state.user;
const {query: initialQuery}=ctx.request;
const query = type === "place" ? {...initialQuery, 'disponibility.espace.users_permissions_user.id':id} : {...initialQuery, company: id };
const populate = type==="place"? [...populateCommon,'company', ]:[...populateCommon,'place']
const populateCommon = [
'disponibility.espace',
'disponibility.espace.users_permissions_user',
'creation_file',
]
const { id, type } = ctx.state.user
const { query: initialQuery } = ctx.request
const query =
type === 'place'
? {
...initialQuery,
'disponibility.espace.users_permissions_user.id': id,
}
: { ...initialQuery, company: id }
const populate =
type === 'place'
? [...populateCommon, 'company']
: [...populateCommon, 'place']

return strapi
.query("application")
.find(
query,
populate
)
.query('application')
.find(query, populate)
.then((res) => {
return Promise.all(
res.map(async (application) => {
Expand All @@ -29,10 +42,30 @@ module.exports = {
type,
applicationId: application.id,
}),
};
})
);
});
}
}),
)
})
},
async generatePdf(ctx) {
const { id } = ctx.params

};
// Fetch the application data from a Strapi service
const applicationData = await strapi.services.application.findOne({ id })

const pdfStream = await ReactPDF.renderToStream(
React.createElement(ApplicationPdf, { applicationData: applicationData }),
)
const pdfBuffer = await new Promise((resolve, reject) => {
const chunks = []
pdfStream.on('data', (chunk) => {
return chunks.push(chunk)
})
pdfStream.on('end', () => resolve(Buffer.concat(chunks)))
pdfStream.on('error', reject)
})

ctx.set('Content-Type', 'application/pdf')
ctx.body = pdfBuffer
},
}
68 changes: 68 additions & 0 deletions back/api/application/documentation/1.0.0/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,74 @@
}
]
}
},
"/applications/{id}/generate-pdf": {
"get": {
"deprecated": false,
"description": "",
"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": [
"Application"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "",
"deprecated": false,
"required": true,
"schema": {
"type": "string"
}
}
]
}
}
},
"components": {
Expand Down
Loading

0 comments on commit d91b19e

Please sign in to comment.