# 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://intercode.gitbook.io/intercode-saas-kit/pages/dashboard/recent-leads-table-widget.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
