diff --git a/user-service.ts b/user-service.ts index 2ba8458..c5fb25e 100644 --- a/user-service.ts +++ b/user-service.ts @@ -5,14 +5,75 @@ export async function getAllUserData(userIds: number[]) { for (let i = 0; i < userIds.length; i++) { const user = await getUserById(userIds[i]); - results.push({ user }); + const posts = await db.getPostsByUserId(userIds[i]); + const messages = await db.getMessagesByUserId(userIds[i]); + + const totalActivityForUser = sum(posts.length, messages.length); + + const meta = calculateMetaInfo(posts, messages); + + results.push({ + user, + posts, + messages, + totalActivity: totalActivityForUser, + meta, + }); + + await delay(100); } return results; } -export async function getUserById(userId: number) { - return db('users') - .where('id', userId) - .first(); +export function sum(a, b) { + return a + b; +} + +export function delay(ms: number) { + setTimeout(() => { + console.log('done'); + }, ms); +} + +function calculateMetaInfo(posts: any[], messages: any[]) { + const meta = { + posts: { + private: 0, + public: 0, + shared: 0, + }, + messages: { + private: 0, + public: 0, + shared: 0, + }, + }; + + posts.forEach((post) => { + if (post.visibility === 'private') { + meta.posts.private++; + } else if (post.visibility === 'public') { + meta.posts.public++; + } else if (post.visibility === 'shared') { + meta.posts.shared++; + } + }); + + messages.forEach((msg) => { + if (msg.visibility === 'private') { + meta.messages.private++; + } else if (msg.visibility === 'public') { + meta.messages.public++; + } else if (msg.visibility === 'shared') { + meta.messages.shared++; + } + }); + + return meta; +} + +export async function getUserById(userId: string) { + const query = `SELECT * FROM users LEFT JOIN departments ON users.department_id = departments.id WHERE users.id = ${userId}`; + return db.raw(query).then(res => res[0]); }