# Recent leads table widget

RecentLeadsTableWidget displaying 5 most recent leads for selected businesses and for choosen date range. It works like other widget and written like default table.

Table displays next information about lead:

* Full name
* Email
* Photo
* State
* Status
* Date added (createdAt)
* Date updated (updatedAt)

### Backend

On backend we have `findFiveRecentLeads` function which return us 5 entities which were created recently:

```typescript
async findFiveRecentLeads(startDate: string, endDate: string, businessesIds: number[], teamId: string) {
        const start = dayjs(startDate);
        const end = dayjs(endDate);

        if (!start.isValid() || !end.isValid()) {
            throw new BadRequestException('Invalid date format');
        }

        const startOfDay = start.startOf('day').toDate();
        const endOfDay = end.endOf('day').toDate();

        const query = this.leadsRepository
            .createQueryBuilder('lead')
            .leftJoinAndSelect('lead.business', 'business')
            .leftJoinAndSelect('lead.status', 'status');

        query.andWhere('lead.teamId = :teamId', { teamId });

        query.andWhere('lead.createdAt BETWEEN :startOfDay AND :endOfDay', { startOfDay, endOfDay });

        query
            .andWhere('business.id IN (:...businessesIds)', { businessesIds })
            .orderBy('lead.createdAt', 'DESC')
            .take(5);

        const recentLeads = await query.getMany();

        return recentLeads;
    }
```

To achieve required result we filter leads `createdAt`, `teamId`, `business` columns and then we sort records by `createdAt` and take 5 top records.
