-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Participation): récupération d'informations initiales
depuis la feuille actuelle. Pour les Coopérateurs, ainsi que des statistiques sommaires refs #2
- Loading branch information
Showing
6 changed files
with
297 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const GoogleSpreadsheet = require("google-spreadsheet"); | ||
const mem = require("mem"); | ||
const parse = require("date-fns/parse"); | ||
|
||
const fetchAllRows = (doc, credentials, sheetTitle, transformResult) => | ||
new Promise((resolve, reject) => { | ||
doc.useServiceAccountAuth(credentials, () => { | ||
doc.getInfo((err, info) => { | ||
if (err) reject(err); | ||
|
||
const sheet = info.worksheets.find(sheet => sheet.title === sheetTitle); | ||
if (!sheet) { | ||
reject(`Aucune feuille trouvée avec le nom "${sheetTitle}"`); | ||
} | ||
|
||
sheet.getRows({}, (err, rows) => { | ||
if (err) reject(err); | ||
resolve(transformResult(rows)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
const UNE_HEURE = 60 * 60 * 1000; | ||
const memoizedRows = mem(fetchAllRows, { | ||
maxAge: UNE_HEURE | ||
}); | ||
|
||
const dateOuVide = value => { | ||
if (value === "-") { | ||
return null; | ||
} | ||
return parse(value, "dd/MM/yyyy", new Date()); | ||
}; | ||
|
||
const formatParticipation = row => { | ||
return { | ||
id: row.id, | ||
email: row.mail.trim(), | ||
|
||
dateDernierePIAF: dateOuVide(row.derniertafeffectué), | ||
dateProchainePIAF: dateOuVide(row.prochaintaf), | ||
|
||
nombrePIAFOk: row.nbtafok === "OK", | ||
nombrePIAFDepuis2018: Number(row.nbtafeffectuésdepuisle2018), | ||
nombrePIAFAttendus: Number(row.nbtafattendus) | ||
}; | ||
}; | ||
|
||
class SuiviParticipationAPI { | ||
constructor(credentials, sheetId, sheetTitle) { | ||
this.doc = new GoogleSpreadsheet(sheetId); | ||
this.credentials = credentials; | ||
this.sheetTitle = sheetTitle; | ||
} | ||
|
||
async getAll() { | ||
return await memoizedRows( | ||
this.doc, | ||
this.credentials, | ||
this.sheetTitle, | ||
rows => | ||
rows.filter(row => row.mail.trim() !== "").map(formatParticipation) | ||
); | ||
} | ||
|
||
async getByEmail(email) { | ||
const all = await this.getAll(); | ||
return all.find(one => one.email === email); | ||
} | ||
|
||
async getAllOk() { | ||
const all = await this.getAll(); | ||
return all.filter(one => one.nombrePIAFOk); | ||
} | ||
|
||
async getAllNonOk() { | ||
const all = await this.getAll(); | ||
return all.filter(one => !one.nombrePIAFOk); | ||
} | ||
} | ||
|
||
module.exports = SuiviParticipationAPI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const { gql } = require("apollo-server"); | ||
const SuiviParticipationAPI = require("./SuiviParticipationAPI"); | ||
|
||
const typeDefs = gql` | ||
extend type Chouettos { | ||
participation: Participation | ||
} | ||
type Participation { | ||
dateDernierePIAF: Date | ||
dateProchainePIAF: Date | ||
nombrePIAFOk: Boolean | ||
nombrePIAFDepuis2018: Int | ||
nombrePIAFAttendus: Int | ||
} | ||
extend type Query { | ||
statsParticipation: StatsParticipation | ||
} | ||
type StatsParticipation { | ||
nombreAJourDePIAF: Int | ||
nombreNonAJourDePIAF: Int | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Query: { | ||
statsParticipation: async (_, __, { dataSources }) => { | ||
const aJour = await dataSources.SuiviParticipationAPI.getAllOk(); | ||
const nonAJour = await dataSources.SuiviParticipationAPI.getAllNonOk(); | ||
|
||
return { | ||
nombreAJourDePIAF: aJour.length, | ||
nombreNonAJourDePIAF: nonAJour.length | ||
}; | ||
} | ||
}, | ||
Chouettos: { | ||
participation: ({ email }, _, { dataSources }) => | ||
dataSources.SuiviParticipationAPI.getByEmail(email) | ||
} | ||
}; | ||
|
||
module.exports = { | ||
typeDefs, | ||
resolvers, | ||
dataSources: () => { | ||
const googleSheetsCredentials = { | ||
type: process.env.GOOGLE_SHEETS_TYPE, | ||
project_id: process.env.GOOGLE_SHEETS_PROJECT_ID, | ||
private_key_id: process.env.GOOGLE_SHEETS_PRIVATE_KEY_ID, | ||
private_key: process.env.GOOGLE_SHEETS_PRIVATE_KEY, | ||
client_email: process.env.GOOGLE_SHEETS_CLIENT_EMAIL, | ||
client_id: process.env.GOOGLE_SHEETS_CLIENT_ID, | ||
auth_uri: process.env.GOOGLE_SHEETS_AUTH_URI, | ||
token_uri: process.env.GOOGLE_SHEETS_TOKEN_URI, | ||
auth_provider_x509_cert_url: | ||
process.env.GOOGLE_SHEETS_AUTH_PROVIDER_X509_CERT_URL, | ||
client_x509_cert_url: process.env.GOOGLE_SHEETS_CLIENT_X509_CERT_URL | ||
}; | ||
|
||
return { | ||
SuiviParticipationAPI: new SuiviParticipationAPI( | ||
googleSheetsCredentials, | ||
process.env.PARTICIPATION_SHEET_ID, | ||
process.env.PARTICIPATION_SHEET_TITLE | ||
) | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.