Skip to content

Commit

Permalink
feat(backend): Add last_active_at field & last_active_at_since parame…
Browse files Browse the repository at this point in the history
…ter (#2261)

- User.last_active_at stores the last date of session activity for the user,
  with day precision
- last_active_at can be used to order users when listing
- last_active_at_since can be used to filter users based on their latest
  activity date
  • Loading branch information
georgepsarakis committed Dec 6, 2023
1 parent d30ea1f commit a6b893d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/funny-lamps-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/backend': minor
---

- Added the `User.last_active_at` timestamp field which stores the latest date of session activity, with day precision. For further details, please consult the [Backend API documentation](https://clerk.com/docs/reference/backend-api/tag/Users#operation/GetUser).
- Added the `last_active_at_since` filtering parameter for the Users listing request. The new parameter can be used to retrieve users that have displayed session activity since the given date. For further details, please consult the [Backend API documentation](https://clerk.com/docs/reference/backend-api/tag/Users#operation/GetUserList).
- Added the `last_active_at` available options for the `orderBy` parameter of the Users listing request. For further details, please consult the [Backend API documentation](https://clerk.com/docs/reference/backend-api/tag/Users#operation/GetUserList).
13 changes: 12 additions & 1 deletion packages/backend/src/api/endpoints/UserApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@ type UserCountParams = {
query?: string;
userId?: string[];
externalId?: string[];
last_active_at_since?: number;
};

type UserListParams = ClerkPaginationRequest<
UserCountParams & {
orderBy?: 'created_at' | 'updated_at' | '+created_at' | '+updated_at' | '-created_at' | '-updated_at';
orderBy?:
| 'created_at'
| 'updated_at'
| '+created_at'
| '+updated_at'
| '-created_at'
| '-updated_at'
| '+last_sign_in_at'
| '+last_active_at'
| '-last_sign_in_at'
| '-last_active_at';
}
>;

Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/api/resources/JSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export interface UserJSON extends ClerkResourceJSON {
unsafe_metadata: UserUnsafeMetadata;
created_at: number;
updated_at: number;
last_active_at: number | null;
}

export interface VerificationJSON extends ClerkResourceJSON {
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/api/resources/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class User {
readonly phoneNumbers: PhoneNumber[] = [],
readonly web3Wallets: Web3Wallet[] = [],
readonly externalAccounts: ExternalAccount[] = [],
readonly lastActiveAt: number | null,
) {}

static fromJSON(data: UserJSON): User {
Expand Down Expand Up @@ -64,6 +65,7 @@ export class User {
(data.phone_numbers || []).map(x => PhoneNumber.fromJSON(x)),
(data.web3_wallets || []).map(x => Web3Wallet.fromJSON(x)),
(data.external_accounts || []).map((x: ExternalAccountJSON) => ExternalAccount.fromJSON(x)),
data.last_active_at,
);
}
}

0 comments on commit a6b893d

Please sign in to comment.