Skip to content

Commit

Permalink
feat(Participation): récupération d'informations initiales
Browse files Browse the repository at this point in the history
depuis la feuille actuelle. Pour les Coopérateurs, ainsi que des
statistiques sommaires

refs #2
  • Loading branch information
real34 committed Oct 14, 2019
1 parent 5229c00 commit f30f11e
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 63 deletions.
28 changes: 28 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,31 @@ LDAP_PASS=SO_MUCH_SECURE_AGAIN!
ANNUAIRE_COOPERATEURS_CSV_URL=https://docs.google.com/spreadsheets/d/e/{{ID_A_REMPLIR}}/pub?single=true&output=csv

PLANNING_CSV_URL=https://docs.google.com/spreadsheets/d/{{ID_A_REMPLIR}}/export?format=csv

PARTICIPATION_SHEET_ID=xxxxxxx
PARTICIPATION_SHEET_TITLE=Suivi
# See https://www.gatsbyjs.org/packages/gatsby-source-google-sheets/?=sheet#step-1-set-up-sheetspermissions
# to retrieve the credentials and configure an access to the Google Sheet.
# Then converts the JSON below in the corresponding env variable
# {
# "type": "service_account",
# "project_id": "suivi-lcc",
# "private_key_id": "xxxxxx",
# "private_key": "-----BEGIN PRIVATE KEY-----xxxxxxx-----END PRIVATE KEY-----\n",
# "client_email": "suivi-lcc@xxxxx",
# "client_id": "xxxxxxx",
# "auth_uri": "https://accounts.google.com/o/oauth2/auth",
# "token_uri": "https://oauth2.googleapis.com/token",
# "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
# "client_x509_cert_url": "xxxxxxx"
# }
GOOGLE_SHEETS_TYPE="service_account"
GOOGLE_SHEETS_PROJECT_ID="xxxxx"
GOOGLE_SHEETS_PRIVATE_KEY_ID="xxxxx"
GOOGLE_SHEETS_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nxxxxxx\n-----END PRIVATE KEY-----\n"
GOOGLE_SHEETS_CLIENT_EMAIL="xxxx"
GOOGLE_SHEETS_CLIENT_ID="xxx"
GOOGLE_SHEETS_AUTH_URI="https://accounts.google.com/o/oauth2/auth"
GOOGLE_SHEETS_TOKEN_URI="https://oauth2.googleapis.com/token"
GOOGLE_SHEETS_AUTH_PROVIDER_X509_CERT_URL="https://www.googleapis.com/oauth2/v1/certs"
GOOGLE_SHEETS_CLIENT_X509_CERT_URL="xxxxxx"
83 changes: 83 additions & 0 deletions Participation/SuiviParticipationAPI.js
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;
72 changes: 72 additions & 0 deletions Participation/index.js
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
)
};
}
};
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Permissions = require("./Permissions");
const Annuaire = require("./Annuaire");
const Authentification = require("./Authentification");
const Planning = require("./Planning");
const Participation = require("./Participation");

const App = {
typeDefs: gql`
Expand Down Expand Up @@ -48,13 +49,15 @@ const schema = makeExecutableSchema({
App.typeDefs,
Annuaire.typeDefs,
Authentification.typeDefs,
Planning.typeDefs
Planning.typeDefs,
Participation.typeDefs
],
resolvers: merge(
App.resolvers,
Annuaire.resolvers,
Authentification.resolvers,
Planning.resolvers
Planning.resolvers,
Participation.resolvers
)
});

Expand All @@ -68,7 +71,12 @@ const server = new ApolloServer({
user
};
},
dataSources: () => merge(Annuaire.dataSources(), Planning.dataSources())
dataSources: () =>
merge(
Annuaire.dataSources(),
Planning.dataSources(),
Participation.dataSources()
)
});

server
Expand Down
Loading

0 comments on commit f30f11e

Please sign in to comment.