Skip to content

Commit

Permalink
chore(api-service,dashboard): Replace create_at sorting with _id for …
Browse files Browse the repository at this point in the history
…subscribers
  • Loading branch information
desiprisg committed Jan 28, 2025
1 parent 94ef255 commit 0f710c0
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 65 deletions.
47 changes: 16 additions & 31 deletions apps/api/src/app/subscribers-v2/subscriber.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,48 +180,33 @@ describe('List Subscriber Cursor Pagination', () => {
});

describe('List Subscriber Sorting', () => {
it('should sort subscribers by createdAt in ascending order', async () => {
it('should sort subscribers by _id in ascending order', async () => {
const uuid = generateUUID();
await create10Subscribers(uuid);

const response = await getListSubscribers({
sortBy: 'createdAt',
sortBy: '_id',
sortDirection: DirectionEnum.ASC,
limit: 10,
});

const timestamps = response.subscribers.map((sub) => new Date(sub.createdAt).getTime());
const sortedTimestamps = [...timestamps].sort((a, b) => a - b);
expect(timestamps).to.deep.equal(sortedTimestamps);
const ids = response.subscribers.map((sub) => sub._id.toString());
const sortedIds = [...ids].sort();
expect(ids).to.deep.equal(sortedIds);
});

it('should sort subscribers by createdAt in descending order', async () => {
it('should sort subscribers by _id in descending order', async () => {
const uuid = generateUUID();
await create10Subscribers(uuid);

const response = await getListSubscribers({
sortBy: 'createdAt',
sortBy: '_id',
sortDirection: DirectionEnum.DESC,
limit: 10,
});

const timestamps = response.subscribers.map((sub) => new Date(sub.createdAt).getTime());
const sortedTimestamps = [...timestamps].sort((a, b) => b - a);
expect(timestamps).to.deep.equal(sortedTimestamps);
});

it('should sort subscribers by subscriberId', async () => {
const uuid = generateUUID();
await create10Subscribers(uuid);

const response = await getListSubscribers({
sortBy: 'subscriberId',
sortDirection: DirectionEnum.ASC,
limit: 10,
});

const ids = response.subscribers.map((sub) => sub.subscriberId);
const sortedIds = [...ids].sort();
const ids = response.subscribers.map((sub) => sub._id.toString());
const sortedIds = [...ids].sort((a, b) => b.localeCompare(a));
expect(ids).to.deep.equal(sortedIds);
});

Expand All @@ -230,25 +215,25 @@ describe('List Subscriber Sorting', () => {
await create10Subscribers(uuid);

const firstPage = await getListSubscribers({
sortBy: 'createdAt',
sortBy: '_id',
sortDirection: DirectionEnum.ASC,
limit: 5,
});

const secondPage = await getListSubscribers({
sortBy: 'createdAt',
sortBy: '_id',
sortDirection: DirectionEnum.ASC,
after: firstPage.next,
limit: 5,
});

const allTimestamps = [
...firstPage.subscribers.map((sub) => new Date(sub.createdAt).getTime()),
...secondPage.subscribers.map((sub) => new Date(sub.createdAt).getTime()),
const allIds = [
...firstPage.subscribers.map((sub) => sub._id.toString()),
...secondPage.subscribers.map((sub) => sub._id.toString()),
];

const sortedTimestamps = [...allTimestamps].sort((a, b) => a - b);
expect(allTimestamps).to.deep.equal(sortedTimestamps);
const sortedIds = [...allIds].sort();
expect(allIds).to.deep.equal(sortedIds);
});
});

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/app/subscribers-v2/subscriber.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class SubscriberController {
after: query.after,
before: query.before,
orderDirection: query.orderDirection || DirectionEnum.DESC,
orderBy: query.orderBy || 'createdAt',
orderBy: query.orderBy || '_id',
email: query.email,
phone: query.phone,
subscriberId: query.subscriberId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export class ListSubscribersCommand extends CursorBasedPaginatedCommand {
@IsOptional()
orderDirection: DirectionEnum = DirectionEnum.DESC;

@IsEnum(['updatedAt', 'createdAt'])
@IsEnum(['updatedAt', '_id'])
@IsOptional()
orderBy: 'updatedAt' | 'createdAt' = 'createdAt';
orderBy: 'updatedAt' | '_id' = '_id';

@IsString()
@IsOptional()
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/components/subscribers/subscriber-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const SubscriberListTable = (props: SubscriberListTableProps) => {
<TableHead>Phone number</TableHead>
<TableHead
sortable
sortDirection={orderBy === 'createdAt' ? orderDirection : false}
onSort={() => toggleSort('createdAt')}
sortDirection={orderBy === '_id' ? orderDirection : false}
onSort={() => toggleSort('_id')}
>
Created at
</TableHead>
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/hooks/use-fetch-subscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function useFetchSubscribers(
email = '',
phone = '',
orderDirection = DirectionEnum.DESC,
orderBy = 'createdAt',
orderBy = '_id',
name = '',
subscriberId = '',
limit = 10,
Expand Down
10 changes: 5 additions & 5 deletions apps/dashboard/src/hooks/use-subscribers-url-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useCallback, useMemo } from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { useDebounce } from '../hooks/use-debounce';

export type SubscribersSortableColumn = 'createdAt' | 'updatedAt';
export type SubscribersSortableColumn = '_id' | 'updatedAt';
export interface SubscribersFilter {
email?: string;
phone?: string;
Expand All @@ -16,15 +16,15 @@ export interface SubscribersFilter {
orderDirection?: DirectionEnum;
}

export const defaultSubscribersFilter: SubscribersFilter = {
export const defaultSubscribersFilter: Required<SubscribersFilter> = {
email: '',
phone: '',
name: '',
subscriberId: '',
limit: 10,
after: '',
before: '',
orderBy: 'createdAt',
orderBy: '_id',
orderDirection: DirectionEnum.DESC,
};

Expand Down Expand Up @@ -54,10 +54,10 @@ export function useSubscribersUrlState(props: UseSubscribersUrlStateProps): Subs
phone: searchParams.get('phone') || '',
name: searchParams.get('name') || '',
subscriberId: searchParams.get('subscriberId') || '',
limit: parseInt(searchParams.get('limit') || '10', 10),
limit: parseInt(searchParams.get('limit') || defaultSubscribersFilter.limit.toString(), 10),
after: searchParams.get('after') || '',
before: searchParams.get('before') || '',
orderBy: (searchParams.get('orderBy') as SubscribersSortableColumn) || 'createdAt',
orderBy: (searchParams.get('orderBy') as SubscribersSortableColumn) || defaultSubscribersFilter.orderBy,
orderDirection: (searchParams.get('orderDirection') as DirectionEnum) || DirectionEnum.DESC,
}),
[searchParams]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class SubscriberRepository extends BaseRepository<SubscriberDBModel, Subs
environmentId: string;
organizationId: string;
limit: number;
sortBy: 'updatedAt' | 'createdAt';
sortBy: 'updatedAt' | '_id';
sortDirection: DirectionEnum;
after?: string;
before?: string;
Expand Down
21 changes: 0 additions & 21 deletions libs/dal/src/repositories/subscriber/subscriber.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,34 +193,13 @@ subscriberSchema.index({
deleted: 1,
});

subscriberSchema.index({
_environmentId: 1,
_organizationId: 1,
createdAt: 1,
_id: 1,
});

subscriberSchema.index({
_environmentId: 1,
_organizationId: 1,
createdAt: -1,
_id: -1,
});

subscriberSchema.index({
_environmentId: 1,
_organizationId: 1,
updatedAt: 1,
_id: 1,
});

subscriberSchema.index({
_environmentId: 1,
_organizationId: 1,
updatedAt: -1,
_id: -1,
});

subscriberSchema.plugin(mongooseDelete, { deletedAt: true, deletedBy: true, overrideMethods: 'all' });

export const Subscriber =
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/dto/subscriber/list-subscribers.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface IListSubscribersRequestDto {

orderDirection: DirectionEnum;

orderBy: 'updatedAt' | 'createdAt';
orderBy: 'updatedAt' | '_id';

email?: string;

Expand Down

0 comments on commit 0f710c0

Please sign in to comment.