-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ personal general 에 dailyActivities 추가
- 기존과는 다른 구조로 제작하였습니다. 앞으로 db 정리하고 이런 방식으로 고치면 어떨까 하는 생각입니다. - daily_logtimes collection 을 추가했습니다. - scale_teams, events_users 로 인해 성능 저하가 심각한 수준이면 별도 collection 으로 분리할 예정입니다. 현재 자체 테스트에선 dev 기준 100 ~ 300ms 정도 나옵니다. - close #388 - close #389
- Loading branch information
Showing
17 changed files
with
470 additions
and
2 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
app/src/api/dailyLogtime/db/dailyLogtime.database.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import type { HydratedDocument } from 'mongoose'; | ||
|
||
export type DailyLogtimeDocument = HydratedDocument<daily_logtimes>; | ||
|
||
@Schema({ collection: 'daily_logtimes' }) | ||
export class daily_logtimes { | ||
@Prop({ required: true }) | ||
userId: number; | ||
|
||
@Prop({ required: true }) | ||
date: Date; | ||
|
||
@Prop({ required: true }) | ||
value: number; | ||
} | ||
|
||
export const dailyLogtimeSchema = SchemaFactory.createForClass(daily_logtimes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { | ||
lookupStage, | ||
type CollectionLookup, | ||
} from 'src/database/mongoose/database.mongoose.aggregation'; | ||
import { events } from './event.database.schema'; | ||
|
||
export const lookupEvents: CollectionLookup = ( | ||
localField, | ||
foreignField, | ||
pipeline, | ||
) => lookupStage(events.name, localField, foreignField, pipeline); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import type { HydratedDocument } from 'mongoose'; | ||
|
||
export type eventDocument = HydratedDocument<events>; | ||
|
||
@Schema({ collection: 'events' }) | ||
export class events { | ||
@Prop({ required: true }) | ||
id: number; | ||
|
||
@Prop({ required: true }) | ||
beginAt: Date; | ||
|
||
@Prop({ required: true }) | ||
endAt: Date; | ||
|
||
@Prop({ required: true }) | ||
name: string; | ||
|
||
@Prop({ required: true }) | ||
description: string; | ||
|
||
@Prop({ required: true }) | ||
location: string; | ||
} | ||
|
||
export const eventSchema = SchemaFactory.createForClass(events); |
11 changes: 11 additions & 0 deletions
11
app/src/api/eventsUser/db/eventsUser.database.aggregate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { | ||
lookupStage, | ||
type CollectionLookup, | ||
} from 'src/database/mongoose/database.mongoose.aggregation'; | ||
import { events_users } from './eventsUser.database.schema'; | ||
|
||
export const lookupEventsUsers: CollectionLookup = ( | ||
localField, | ||
foreignField, | ||
pipeline, | ||
) => lookupStage(events_users.name, localField, foreignField, pipeline); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Prop, Schema } from '@nestjs/mongoose'; | ||
|
||
@Schema({ collection: 'events_users' }) | ||
export class events_users { | ||
@Prop({ required: true }) | ||
id: number; | ||
|
||
@Prop({ required: true }) | ||
eventId: number; | ||
|
||
@Prop({ required: true }) | ||
userId: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/src/page/personal/general/activity/args/personal.general.dailyActivity.args.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ArgsType, Field } from '@nestjs/graphql'; | ||
import { IsOptional, Max, Min } from 'class-validator'; | ||
|
||
@ArgsType() | ||
export class GetPersonalGeneralDailyActivitiesArgs { | ||
@IsOptional() | ||
@Min(2000) | ||
@Max(2100) | ||
@Field({ nullable: true }) | ||
year?: number; | ||
} |
128 changes: 128 additions & 0 deletions
128
app/src/page/personal/general/activity/db/personal.general.dailyActivity.dao.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import type { Model } from 'mongoose'; | ||
import { daily_logtimes } from 'src/api/dailyLogtime/db/dailyLogtime.database.schema'; | ||
import { lookupEvents } from 'src/api/event/db/event.database.aggregate'; | ||
import { events } from 'src/api/event/db/event.database.schema'; | ||
import { events_users } from 'src/api/eventsUser/db/eventsUser.database.schema'; | ||
import { scale_team } from 'src/api/scaleTeam/db/scaleTeam.database.schema'; | ||
import { | ||
DailyActivityType, | ||
type DailyActivityRecordUnion, | ||
type DailyDefaultRecord, | ||
type DailyLogtimeRecord, | ||
} from '../models/personal.general.dailyActivity.model'; | ||
|
||
export type DailyActivityDto = { | ||
date: Date; | ||
records: (typeof DailyActivityRecordUnion)[]; | ||
}; | ||
|
||
export type DailyActivityDao = { | ||
findAllRecordByDate: (args: { | ||
userId: number; | ||
start: Date; | ||
end: Date; | ||
}) => Promise<(typeof DailyActivityRecordUnion)[]>; | ||
}; | ||
|
||
@Injectable() | ||
export class DailyActivityDaoImpl implements DailyActivityDao { | ||
constructor( | ||
@InjectModel(scale_team.name) | ||
private readonly scaleTeamModel: Model<scale_team>, | ||
) {} | ||
|
||
async findAllRecordByDate({ | ||
userId, | ||
start, | ||
end, | ||
}: { | ||
userId: number; | ||
start: Date; | ||
end: Date; | ||
}): Promise<((DailyLogtimeRecord & { date: Date }) | DailyDefaultRecord)[]> { | ||
return await this.scaleTeamModel | ||
.aggregate<(DailyLogtimeRecord & { date: Date }) | DailyDefaultRecord>() | ||
.match({ | ||
$or: [{ 'corrector.id': userId }, { 'correcteds.id': userId }], | ||
filledAt: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}) | ||
.sort({ filledAt: 1 }) | ||
.project({ | ||
_id: 0, | ||
id: 1, | ||
at: '$filledAt', | ||
type: { | ||
$cond: { | ||
if: { $eq: ['$corrector.id', userId] }, | ||
then: DailyActivityType.CORRECTOR, | ||
else: DailyActivityType.CORRECTED, | ||
}, | ||
}, | ||
}) | ||
.unionWith({ | ||
coll: daily_logtimes.name, | ||
pipeline: [ | ||
{ | ||
$match: { | ||
userId, | ||
date: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$project: { | ||
_id: 0, | ||
date: '$date', | ||
type: { $literal: DailyActivityType.LOGTIME }, | ||
value: 1, | ||
}, | ||
}, | ||
], | ||
}) | ||
.unionWith({ | ||
coll: events_users.name, | ||
pipeline: [ | ||
{ | ||
$match: { | ||
userId, | ||
}, | ||
}, | ||
lookupEvents('eventId', 'id', [ | ||
{ | ||
$match: { | ||
date: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}, | ||
}, | ||
]), | ||
{ | ||
$project: { | ||
_id: 0, | ||
id: 1, | ||
at: { $first: `$${events.name}.endAt` }, | ||
type: { $literal: DailyActivityType.EVENT }, | ||
}, | ||
}, | ||
{ | ||
$match: { | ||
at: { $ne: null }, | ||
}, | ||
}, | ||
{ | ||
$sort: { | ||
at: 1, | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/src/page/personal/general/activity/models/personal.general.dailyActivity.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
Field, | ||
ObjectType, | ||
createUnionType, | ||
registerEnumType, | ||
} from '@nestjs/graphql'; | ||
|
||
export enum DailyActivityType { | ||
CORRECTOR, | ||
CORRECTED, | ||
EVENT, | ||
LOGTIME, | ||
} | ||
|
||
registerEnumType(DailyActivityType, { | ||
name: 'DailyAcitivtyType', | ||
}); | ||
|
||
@ObjectType() | ||
export class DailyLogtimeRecord { | ||
@Field((_type) => DailyActivityType) | ||
type: DailyActivityType.LOGTIME; | ||
|
||
@Field() | ||
value: number; | ||
} | ||
|
||
@ObjectType() | ||
export class DailyDefaultRecord { | ||
@Field((_type) => DailyActivityType) | ||
type: Exclude<DailyActivityType, DailyActivityType.LOGTIME>; | ||
|
||
@Field() | ||
id: number; | ||
|
||
@Field() | ||
at: Date; | ||
} | ||
|
||
export const DailyActivityRecordUnion = createUnionType({ | ||
name: 'DailyActivityRecord', | ||
types: () => [DailyLogtimeRecord, DailyDefaultRecord] as const, | ||
resolveType: (value) => { | ||
switch (value.type) { | ||
case DailyActivityType.LOGTIME: | ||
return DailyLogtimeRecord; | ||
default: | ||
return DailyDefaultRecord; | ||
} | ||
}, | ||
}); | ||
|
||
@ObjectType() | ||
export class DailyActivity { | ||
@Field() | ||
date: Date; | ||
|
||
@Field((_type) => [DailyActivityRecordUnion]) | ||
records: (typeof DailyActivityRecordUnion)[]; | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/page/personal/general/activity/personal.general.dailyActivity.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { | ||
ScaleTeamSchema, | ||
scale_team, | ||
} from 'src/api/scaleTeam/db/scaleTeam.database.schema'; | ||
import { CacheUtilModule } from 'src/cache/cache.util.module'; | ||
import { DailyActivityDaoImpl } from './db/personal.general.dailyActivity.dao'; | ||
import { PersonalGeneralDailyActivityService } from './personal.general.dailyActivity.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{ name: scale_team.name, schema: ScaleTeamSchema }, | ||
]), | ||
CacheUtilModule, | ||
], | ||
providers: [PersonalGeneralDailyActivityService, DailyActivityDaoImpl], | ||
exports: [PersonalGeneralDailyActivityService, MongooseModule], | ||
}) | ||
// eslint-disable-next-line | ||
export class PersonalGeneralDailyActivityModule {} |
Oops, something went wrong.