Skip to content

Commit 068330a

Browse files
authored
Merge pull request #11 from Dialogue-Bot/DIAL-11-Add-DB-channeltype-channel
DIAL-11-Add-DB-channeltype-channel
2 parents 71f53b8 + 45f0e1c commit 068330a

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

server/src/controllers/channels.controller.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { LocaleService } from "@/i18n/ctx";
44
import { ChannelService } from "@/services/channels.service";
55
import { catchAsync } from "@/utils/catch-async";
66
import { plainToClass } from "class-transformer";
7-
import { validateOrReject } from "class-validator";
87
import { StatusCodes } from "http-status-codes";
98
import Container from "typedi";
109

@@ -42,9 +41,8 @@ export class ChannelController {
4241

4342
public getChannelsPaging = catchAsync(async (req, res) => {
4443
const paging = plainToClass(PagingDTO, req.query);
45-
await validateOrReject(paging);
4644

47-
const result = await this.channelService.getChannelsPaging(paging);
48-
res.status(StatusCodes.OK).json({ result });
45+
const data = await this.channelService.getChannelsPaging(paging);
46+
res.status(StatusCodes.OK).json({ data });
4947
})
5048
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export interface ChannelExtend {
2-
id: number;
2+
id: string;
33
contactId: string;
44
contactName: string;
55
channelType: string;
66
credentials: string;
77
active: boolean;
8-
createdAt: string;
9-
updateAt: string;
8+
createdAt: Date;
9+
updateAt: Date;
1010
}

server/src/services/channels.service.ts

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { HttpException } from "@/exceptions/http-exception";
77
import { LocaleService } from "@/i18n/ctx";
88
import { ChannelExtend } from "@/interfaces/channels.interface";
99
import { Paging } from "@/interfaces/paging.interface";
10-
import { Helper } from "@/utils/helper";
11-
import { asc, desc, eq, isNotNull, like } from "drizzle-orm";
10+
import { and, asc, desc, eq, like, sql } from "drizzle-orm";
1211
import { StatusCodes } from "http-status-codes";
1312
import { Inject, Service } from "typedi";
1413

@@ -126,27 +125,67 @@ export class ChannelService {
126125
});
127126
}
128127

129-
public async getChannelsPaging(paging: PagingDTO): Promise<Paging<ChannelExtend>> {
130-
const query = await db
131-
.select()
128+
public async getChannelsPaging(paging: PagingDTO): Promise<Paging<any>> {
129+
const offset = paging.page && ((paging.page - 1) * paging.limit) || 0;
130+
// const [query, count] =
131+
// await Promise.all([
132+
// db
133+
// .select({
134+
// id: channels.id,
135+
// contactId: channels.contactId,
136+
// name: channels.contactName,
137+
// channelType: channelTypes.description,
138+
// credentials: channels.credentials,
139+
// active: channels.active,
140+
// createdAt: channels.createdAt,
141+
// updateAt: channels.updatedAt,
142+
// })
143+
// .from(channels)
144+
// .where(
145+
// and(
146+
// like(channels.contactId, `%${paging.q}%`),
147+
// eq(channels.deleted, false)
148+
// )
149+
// )
150+
// .innerJoin(channelTypes, eq(channels.channelTypeId, channelTypes.id))
151+
// .orderBy(paging.sortType === 'asc' ? asc(channels[paging.orderBy]) : desc(channels[paging.orderBy]))
152+
// .offset(offset)
153+
// .limit(paging.limit),
154+
// db
155+
// .select({ count: sql<number>`cast(count(${channels.id}) as integer)` })
156+
// .from(channels)
157+
// .where(eq(channels.deleted, false)),
158+
// ]);
159+
160+
const result: ChannelExtend[] = await db
161+
.select({
162+
id: channels.id,
163+
contactId: channels.contactId,
164+
contactName: channels.contactName,
165+
name: channels.contactName,
166+
channelType: channelTypes.description,
167+
credentials: channels.credentials,
168+
active: channels.active,
169+
createdAt: channels.createdAt,
170+
updateAt: channels.updatedAt,
171+
})
132172
.from(channels)
133-
.where(paging.q ? like(channels.contactId, `%${paging.q}%`) : isNotNull(channels.contactId))
173+
.where(
174+
and(
175+
like(channels.contactId, `%${paging.q}%`),
176+
eq(channels.deleted, false)
177+
)
178+
)
134179
.innerJoin(channelTypes, eq(channels.channelTypeId, channelTypes.id))
135180
.orderBy(paging.sortType === 'asc' ? asc(channels[paging.orderBy]) : desc(channels[paging.orderBy]))
136-
.offset(paging.page * paging.limit)
181+
.offset(offset)
137182
.limit(paging.limit);
138183

139-
const data: ChannelExtend[] = query.map((rs: any) => ({
140-
id: rs.channels?.id,
141-
contactId: rs.channels?.contactId,
142-
contactName: rs.channels?.contactName,
143-
channelType: rs.channel_types?.description,
144-
credentials: rs.channels?.credentials ?? null,
145-
active: rs.channels?.active,
146-
createdAt: Helper.formatDate(rs.channels?.createdAt),
147-
updateAt: Helper.formatDate(rs.channels?.updateAt),
148-
}));
149-
150-
return { items: data, totalItems: data.length };
184+
const count = await db
185+
.select({ count: sql<number>`cast(count(${channels.id}) as integer)` })
186+
.from(channels)
187+
.where(eq(channels.deleted, false));
188+
189+
return { items: result, totalItems: count[0].count };
151190
}
152191
}

0 commit comments

Comments
 (0)