* Format statistics as Slack message blocks * @param {ProcessedStats} stats - Grouped statistics by language and user * @param {string} projectName - Project name * @param {string} from - Start date in ISO 8601 format * @param {string} to - End date in ISO 8601 format * @param {string} generate
(stats, projectName, from, to, generatedAt)
| 172 | * @returns {object} Slack message payload with blocks |
| 173 | */ |
| 174 | function formatSlackMessage(stats, projectName, from, to, generatedAt) { |
| 175 | const monthName = new Date(from).toLocaleString('en-US', {month: 'long', year: 'numeric'}); |
| 176 | // Format dates as YYYY-MM-DD for display |
| 177 | const fromDate = from.split('T')[0]; |
| 178 | const toDate = to.split('T')[0]; |
| 179 | |
| 180 | const blocks = [ |
| 181 | { |
| 182 | type: 'header', |
| 183 | text: { |
| 184 | type: 'plain_text', |
| 185 | text: `📊 Crowdin Translation Stats for ${monthName}`, |
| 186 | emoji: true, |
| 187 | }, |
| 188 | }, |
| 189 | { |
| 190 | type: 'section', |
| 191 | text: { |
| 192 | type: 'mrkdwn', |
| 193 | text: `*Project:* ${projectName}\n*Period:* ${fromDate} to ${toDate}`, |
| 194 | }, |
| 195 | }, |
| 196 | { |
| 197 | type: 'divider', |
| 198 | }, |
| 199 | ]; |
| 200 | |
| 201 | if (Object.keys(stats).length === 0) { |
| 202 | blocks.push({ |
| 203 | type: 'section', |
| 204 | text: { |
| 205 | type: 'mrkdwn', |
| 206 | text: '❌ No translation activity found for this period.', |
| 207 | }, |
| 208 | }); |
| 209 | } else { |
| 210 | // Slack section text is limited to 3000 characters. Keep a safety margin. |
| 211 | const MAX_SECTION_CHARS = 2900; |
| 212 | for (const [language, users] of Object.entries(stats)) { |
| 213 | const userEntries = Object.entries(users) |
| 214 | .filter(([, counts]) => counts.translated > 0 || counts.approved > 0) |
| 215 | .sort(([, a], [, b]) => b.translated + b.approved - (a.translated + a.approved)); |
| 216 | |
| 217 | if (userEntries.length === 0) { |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | const totalTranslated = userEntries.reduce((sum, [, u]) => sum + u.translated, 0); |
| 222 | const totalApproved = userEntries.reduce((sum, [, u]) => sum + u.approved, 0); |
| 223 | const header = `*${language}* (Total: ${totalTranslated} translated, ${totalApproved} approved)`; |
| 224 | |
| 225 | // Build bullet lines |
| 226 | const lines = userEntries.map( |
| 227 | ([user, counts]) => `• *${user}*: ${counts.translated} translated, ${counts.approved} approved`, |
| 228 | ); |
| 229 | |
| 230 | // Chunk lines so each section stays under limit |
| 231 | let current = header; |