Skip to content

Commit

Permalink
Normalisation saisie utilisatuer
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara Martini committed Nov 7, 2023
1 parent ac7f3b4 commit 8e9b63a
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions bots/attendance/attendanceV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const userAttendances = [];
let emojiResponse = [];
const weekResponse = [];
const days = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi'];
const presenceValue = 'v';
const absenceValue = 'x';
const optionalValue = '?';

for (const day of days) {
weekResponse[day] = 0;
Expand All @@ -29,9 +32,9 @@ function generateEmojiResponse(textAfterCommand) {
const emojiArray = [];

for (const char of textAfterCommand) {
if (char === 'v') {
if (char === presenceValue) {
emojiArray.push('✅');
} else if (char === 'x') {
} else if (char === absenceValue) {
emojiArray.push('❌');
} else {
emojiArray.push('❓');
Expand All @@ -40,54 +43,66 @@ function generateEmojiResponse(textAfterCommand) {
return emojiArray.join('');
}

function generateWeekResponseString(day, vCount, qCount) {
let response = `${day}: ${vCount}`;
if (qCount) {
function generateWeekResponseString(day, presenceCount, optionalCount) {
let response = `${day}: ${presenceCount}`;
if (optionalCount) {
response += ` (ou `;
if (qCount > 1) {
if (optionalCount > 1) {
response += ' ';
}
response += `${vCount + qCount}`;
response += `${presenceCount + optionalCount}`;
response += ')';
}
return response;
}

function generateWeekResponse() {
for (const day of days) {
weekResponse[day] = { v: 0, '?': 0 };
weekResponse[day] = { presenceCount: 0, optionalCount: 0 };
}

for (const userId of Object.keys(userAttendances)) {
const userAttendance = userAttendances[userId];
for (const [index, day] of days.entries()) {
for (const attendance of userAttendance) {
switch (attendance[index]) {
case 'v': {
weekResponse[day]['v']++;
case presenceValue: {
weekResponse[day].presenceCount++;
break;
}
case '?': {
weekResponse[day]['?']++;
weekResponse[day].optionalCount++;
break;
}
}
}
}
}
const weekResponseArray = days.map((day) => {
const vCount = weekResponse[day]['v'];
const qCount = weekResponse[day]['?'];
return generateWeekResponseString(day, vCount, qCount);
return generateWeekResponseString(day, weekResponse[day].presenceCount, weekResponse[day].optionalCount);
});

return weekResponseArray.join('\n');
}

function normalizeUserAvailability(value) {
let result = '';
for (const dayAvailabity of value) {
if (dayAvailabity === 'v' || dayAvailabity === 'V' || dayAvailabity === '1') {
result += presenceValue;
} else if (dayAvailabity === 'x' || dayAvailabity === 'X' || dayAvailabity === '0') {
result += absenceValue;
} else {
result += optionalValue;
}
}
return result;
}

app.command('/attendance', async ({ ack, body, client }) => {
await ack();

const textAfterCommand = body.text;
const userAvailabity = normalizeUserAvailability(body.text);
const userId = body.user_id;

const userInfo = await client.users.info({
Expand All @@ -105,9 +120,9 @@ app.command('/attendance', async ({ ack, body, client }) => {
}
}

updateUserAttendance(userId, textAfterCommand);
updateUserAttendance(userId, userAvailabity);

const userResponse = generateEmojiResponse(textAfterCommand);
const userResponse = generateEmojiResponse(userAvailabity);

emojiResponse.push(`${userName} ${userLastName} : ${userResponse}`);

Expand Down

0 comments on commit 8e9b63a

Please sign in to comment.