some title

descr

by Vu Nguyen

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.13.0/polyfill.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<!-- No need to change this -->
<!-- Mocha test output goes here. -->
<div id="mocha"></div>

Babel + JSX

/*
* Implement a function that returns the summary of the current user's latest conversations,
* sorted by the latest message's timestamp (most recent conversation first).
*
* Make sure to have good unit tests in addition to the provided integration test!
*
* You have the following REST API available (base URL provided as a constant):
*
* Get current user's conversations: GET /conversations
* Get messages in a conversation: GET /conversations/:conversation_id/messages
* Get user by ID: GET /users/:user_id
*
* The result should be an array of objects of the following shape/type:
* {
*   id : string;
*   latest_message: {
*     body : string;
*     from_user : {
*       id: string;
*       avatar_url: string;
*     };
*     created_at : ISOString;
*   };
* }
*
*/
const API_BASE_URL = "https://rechomework.prd.mz.internal.unity3d.com/api";

async function getRecentConversationSummaries () {
  // TODO: Implement this
  try {
    const conversationUrl = `${API_BASE_URL}/conversations`;
    const usersUrl= `${API_BASE_URL}/users`;

		const [conversations, users] = await Promise.all([
      (await fetch(conversationUrl)).json(),
      (await fetch(usersUrl)).json(),
    ])
    const recentConversationSummaries = await getSummaries(conversations, users);
    return recentConversationSummaries;
  } catch (e) {
  	const msg = 'getRecentConversationSummaries failed';
    throw new Error(msg);
  }

}

function getLatestMessageInMessages(messages) {
	if (!messages || messages.length === 0) {
  	return null;
  }
  let latestMessage = messages[0];

  for (let i = 1; i < messages.length; i++) {
    if(messages[i].created_at > latestMessage.created_at) {
      latestMessage = messages[i];
    }
  }
  return latestMessage;
}

async function getSummaries(conversations, users) {
  return Promise.all(
    conversations.map(conversation => getLatestSummary(conversation, users))
  )
}

async function getLatestSummary(conversation, users) {
  try {
    const messageUrl=...