Skip to content

Commit

Permalink
[fix] check event summary data error
Browse files Browse the repository at this point in the history
  • Loading branch information
Soecka committed Jan 3, 2025
1 parent 34e678c commit 36e0682
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 87 deletions.
71 changes: 42 additions & 29 deletions src/controller/CheckEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
import { ResponseSchema } from "routing-controllers-openapi";

import {
ActivityAgendaCheckInListChunk,
ActivityAgendaCheckInSummary,
ActivityCheckInListChunk,
ActivityCheckInSummary,
BaseFilter,
CheckEvent,
CheckEventChunk,
Expand All @@ -22,6 +25,7 @@ import {
dataSource,
User,
UserActivityCheckInListChunk,
UserActivityCheckInSummary,
} from "../model";
import { ActivityLogController } from "./ActivityLog";
import { FindOptionsWhere } from "typeorm";
Expand All @@ -30,6 +34,13 @@ import { FindOptionsWhere } from "typeorm";
export class CheckEventController {
store = dataSource.getRepository(CheckEvent);
userStore = dataSource.getRepository(User);
userActivityCheckInStore = dataSource.getRepository(
UserActivityCheckInSummary,
);
activityAgendaCheckInStore = dataSource.getRepository(
ActivityAgendaCheckInSummary,
);
activityCheckInStore = dataSource.getRepository(ActivityCheckInSummary);

@Post()
@Authorized()
Expand All @@ -38,7 +49,7 @@ export class CheckEventController {
@CurrentUser() createdBy: User,
@Body() { user: id, ...data }: CheckEventInput,
) {
if (createdBy.id === id) throw new ForbiddenError("No self-checking");
// if (createdBy.id === id) throw new ForbiddenError("No self-checking");

const user = await this.userStore.findOne({ where: { id } });

Expand Down Expand Up @@ -86,46 +97,48 @@ export class CheckEventController {

@Get("/user/:id")
@ResponseSchema(UserActivityCheckInListChunk)
getCheckEventList(
async getCheckEventList(
@Param("id") id: number,
@QueryParams() filter: BaseFilter,
@QueryParams() { pageSize = 10, pageIndex = 1 }: BaseFilter,
) {
return this.queryList(
{ user: { id } },
filter,
["user"],
);
const [list, count] = await this.userActivityCheckInStore.findAndCount({
where: { userId: id },
skip: pageSize * (pageIndex - 1),
take: pageSize,
});

for (
let i = 0, item: UserActivityCheckInSummary;
(item = list[i]);
i++
) {
item.user = await this.userStore.findOneBy({ id: item.userId });
}
return { list, count };
}

@Get("/activity/:id")
@ResponseSchema(ActivityCheckInListChunk)
getActivityCheckEventList(
@ResponseSchema(ActivityAgendaCheckInListChunk)
async getActivityCheckEventList(
@Param("id") id: string,
@QueryParams() filter: BaseFilter,
@QueryParams() { pageSize = 10, pageIndex = 1 }: BaseFilter,
) {
return this.queryList({ activityId: id }, filter);
const [list, count] = await this.activityAgendaCheckInStore
.findAndCount({
where: { activityId: id },
skip: pageSize * (pageIndex - 1),
take: pageSize,
});
return { list, count };
}

@Get("/activity")
@ResponseSchema(ActivityCheckInListChunk)
getAgendaCheckEventList(
@Param("id") id: string,
@QueryParams() filter: BaseFilter,
) {
return this.queryList({ agendaId: id }, filter);
}

async queryList(
where: FindOptionsWhere<CheckEvent>,
{ pageSize, pageIndex, sort }: BaseFilter,
relations?: string[],
async getAgendaCheckEventList(
@QueryParams() { pageSize = 10, pageIndex = 1 }: BaseFilter,
) {
const skip = pageSize * (pageIndex - 1);

const [list, count] = await this.store.findAndCount({
where,
relations,
skip,
const [list, count] = await this.activityCheckInStore.findAndCount({
skip: pageSize * (pageIndex - 1),
take: pageSize,
});
return { list, count };
Expand Down
37 changes: 8 additions & 29 deletions src/model/CheckEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,18 @@ export class CheckEventChunk implements ListChunk<CheckEvent> {
connection
.createQueryBuilder()
.from(CheckEvent, "ce")
.leftJoin("ce.user", "user")
.groupBy("user.id, ce.activityId,")
.select("user.id", "userId")
.addSelect("user.mobilePhone", "userMobilePhone")
.addSelect("user.nickName", "userNickName")
.addSelect("user.email", "userEmail")
.addSelect("user.avatar", "userAvatar")
.addSelect("ce.activityId", "activityId")
.groupBy("ce.user.id, ce.activityId")
.select("ce.activityId", "activityId")
.addSelect("ce.user.id", "userId")
.addSelect("ce.activityName", "activityName")
.addSelect("COUNT(ce.id)", "checkCount"),
})
export class UserActivityCheckInSummary {
@ViewColumn()
@IsInt()
@Min(1)
userId: number;

@ViewColumn()
userMobilePhone: string;

@ViewColumn()
userNickName: string;

@ViewColumn()
userEmail: string;

@ViewColumn()
userAvatar: string;

@ViewColumn()
activityId: string;

Expand All @@ -131,15 +116,9 @@ export class UserActivityCheckInSummary {
@ViewColumn()
checkCount: number;

get user(): User {
return {
id: this.userId,
mobilePhone: this.userMobilePhone,
nickName: this.userNickName,
email: this.userEmail,
avatar: this.userAvatar,
} as User;
}
@Type(() => User)
@ValidateNested()
user: User;
}

@ViewEntity({
Expand Down
71 changes: 42 additions & 29 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
import { ConnectionOptions, parse } from 'pg-connection-string';
import { DataSource } from 'typeorm';
import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions';
import { ConnectionOptions, parse } from "pg-connection-string";
import { DataSource } from "typeorm";
import { SqliteConnectionOptions } from "typeorm/driver/sqlite/SqliteConnectionOptions";

import { DATABASE_URL, isProduct } from '../utility';
import { CheckEvent } from './CheckEvent';
import { User } from './User';
import { ActivityLog } from './ActivityLog';
import { DATABASE_URL, isProduct } from "../utility";
import {
ActivityAgendaCheckInSummary,
ActivityCheckInSummary,
CheckEvent,
UserActivityCheckInSummary,
} from "./CheckEvent";
import { User } from "./User";
import { ActivityLog, UserRank } from "./ActivityLog";

export * from './Base';
export * from './CheckEvent';
export * from './Crawler';
export * from './KToken';
export * from './User';
export * from './ActivityLog';
export * from "./Base";
export * from "./CheckEvent";
export * from "./Crawler";
export * from "./KToken";
export * from "./User";
export * from "./ActivityLog";

const { ssl, host, port, user, password, database } = isProduct
? parse(DATABASE_URL)
: ({} as ConnectionOptions);

const commonOptions: Pick<
SqliteConnectionOptions,
'logging' | 'synchronize' | 'entities' | 'migrations'
"logging" | "synchronize" | "entities" | "migrations"
> = {
logging: true,
synchronize: true,
entities: [User, ActivityLog, CheckEvent],
migrations: [`${isProduct ? '.tmp' : 'migration'}/*.ts`]
entities: [
User,
UserRank,
ActivityLog,
CheckEvent,
UserActivityCheckInSummary,
ActivityAgendaCheckInSummary,
ActivityCheckInSummary,
],
migrations: [`${isProduct ? ".tmp" : "migration"}/*.ts`],
};

export const dataSource = isProduct
? new DataSource({
type: 'postgres',
ssl: ssl as boolean,
host,
port: +port,
username: user,
password,
database,
...commonOptions
})
type: "postgres",
ssl: ssl as boolean,
host,
port: +port,
username: user,
password,
database,
...commonOptions,
})
: new DataSource({
type: 'sqlite',
database: '.tmp/test.db',
...commonOptions
});
type: "sqlite",
database: ".tmp/test.db",
...commonOptions,
});

0 comments on commit 36e0682

Please sign in to comment.