Skip to content

Commit cee3acc

Browse files
authored
[ADD] userImage and dashboard user (#78)
1 parent 66b3aa0 commit cee3acc

File tree

8 files changed

+132
-13
lines changed

8 files changed

+132
-13
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
},
6363
"rules": {
6464
"@typescript-eslint/no-var-requires": 0,
65+
"@typescript-eslint/ban-ts-ignore": 0,
66+
"@typescript-eslint/ban-ts-comment": 0,
6567
"max-len": [
6668
"error",
6769
{

src/api/controllers/idea.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export class IdeaController {
154154

155155
const problemArea = new IdeaProblemArea();
156156
problemArea.id = req.body.problemAreaId;
157+
// @typescript-eslint/ban-ts-comment
157158
// @ts-ignore
158159
delete req.body.problemAreaId;
159160
let updated = await this.ideaService.update(req.params.id, {
@@ -183,7 +184,7 @@ export class IdeaController {
183184
}
184185

185186
@Controller({route: '/leaderboards/team'})
186-
export class LeaderboardController {
187+
export class LeaderboardTeamController {
187188
constructor(private ideaService: IdeaService) {}
188189

189190
@GET({

src/api/controllers/user.ts

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import UserService from '../services/user';
55
import authenticate from '../hooks/onRequest/authentication';
66
import {User} from '../entity/user';
77
import {commonQueryString, commonParams} from '../schemas/common';
8-
import {userSchema, userProfileSchema} from '../schemas/user';
8+
import {userSchema, userProfileSchema, userLeaderSchema} from '../schemas/user';
99

1010
@Controller({route: '/users'})
11-
export default class UserController {
11+
export class UserController {
1212
constructor(private service: UserService) {}
1313

1414
@GET({
@@ -214,4 +214,80 @@ export default class UserController {
214214

215215
return user;
216216
}
217+
218+
@GET({
219+
url: '/account',
220+
options: {
221+
schema: {
222+
response: {200: userSchema},
223+
},
224+
onRequest: authenticate,
225+
},
226+
})
227+
async getUserAccount(req: AuthenticatedRequest): Promise<User | undefined> {
228+
const user = await this.service.getOne({id: req.user.user.id});
229+
if (!user) throw {statusCode: 404, message: 'Data not found'};
230+
return user;
231+
}
232+
233+
@PUT({
234+
url: '/image',
235+
options: {
236+
schema: {
237+
response: {200: userSchema},
238+
},
239+
onRequest: authenticate,
240+
},
241+
})
242+
async updateUserImage(
243+
req: AuthenticatedRequest<{
244+
Body: {imageUrl: string};
245+
}>,
246+
): Promise<User | undefined> {
247+
const user = await this.service.updateById(req.user.user.id, {
248+
imageUrl: req.body.imageUrl,
249+
});
250+
if (!user) throw {statusCode: 404, message: 'Data not found'};
251+
return user;
252+
}
253+
254+
@PUT({
255+
url: '/fcm-token',
256+
options: {
257+
schema: {
258+
response: {200: userSchema},
259+
},
260+
onRequest: authenticate,
261+
},
262+
})
263+
async updateUserFCMToken(
264+
req: AuthenticatedRequest<{
265+
Body: {fcmToken: string};
266+
}>,
267+
): Promise<User | undefined> {
268+
const user = await this.service.updateById(req.user.user.id, {
269+
fcmToken: req.body.fcmToken,
270+
});
271+
if (!user) throw {statusCode: 404, message: 'Data not found'};
272+
return user;
273+
}
274+
}
275+
276+
@Controller({route: '/leaderboards/user'})
277+
export class LeaderboardUserController {
278+
constructor(private service: UserService) {}
279+
280+
@GET({
281+
url: '/',
282+
options: {
283+
schema: {
284+
querystring: commonQueryString,
285+
response: {200: {type: 'array', items: userLeaderSchema}},
286+
},
287+
},
288+
})
289+
async getUserLeaders(): Promise<User[]> {
290+
const leaderUsers = await this.service.getUserLeader();
291+
return leaderUsers;
292+
}
217293
}

src/api/entity/user.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export class User {
5050
@Column({type: 'bigint', default: 0})
5151
markodingPoint: number;
5252

53+
@Column('text')
54+
fcmToken: string;
55+
56+
@Column('text')
57+
imageUrl: string;
58+
5359
@OneToMany(() => UserPoint, (point) => point.user)
5460
points: UserPoint[];
5561
}
@@ -63,6 +69,8 @@ export type UserInput = Omit<
6369
| 'skilvulPoint'
6470
| 'markodingPoint'
6571
| 'points'
72+
| 'imageUrl'
73+
| 'fcmToken'
6674
>;
6775

6876
export type UserUpdateInput = Omit<

src/api/schemas/user.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const properties = {
1111
type: 'number',
1212
nullable: true,
1313
},
14+
fcmToken: {type: 'string'},
15+
imageUrl: {type: 'string'},
1416
};
1517

1618
export const userSchema = {
@@ -40,3 +42,20 @@ export const userProfileSchema = {
4042
},
4143
},
4244
};
45+
46+
export const userLeaderSchema = {
47+
type: 'object',
48+
properties: {
49+
id: {type: 'string'},
50+
name: {type: 'string'},
51+
imageUrl: {type: 'string'},
52+
skilvulPoint: {
53+
type: 'number',
54+
nullable: true,
55+
},
56+
markodingPoint: {
57+
type: 'number',
58+
nullable: true,
59+
},
60+
},
61+
};

src/api/services/event.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@ export default class EventService {
2222
async getAll(queryString: CommonQueryString): Promise<[Event[], number]> {
2323
const {limit, offset, sort} = queryString;
2424
let query = this.repository.createQueryBuilder('events');
25-
25+
query.addSelect('start_date > now() as is_active');
2626
if (sort) {
2727
if (sort.startsWith('-')) {
2828
query = query.orderBy('events.title', 'DESC');
2929
} else {
3030
query = query.orderBy('events.title', 'ASC');
3131
}
32+
} else {
33+
query.addOrderBy('is_active', 'DESC');
34+
query.addOrderBy('events.start_date', 'ASC');
3235
}
33-
34-
return query
35-
.offset(offset)
36-
.limit(limit)
37-
.addOrderBy('events.start_date', 'ASC')
38-
.getManyAndCount();
36+
return query.offset(offset).limit(limit).getManyAndCount();
3937
}
4038

4139
async store(event: Partial<Event>): Promise<Event> {

src/api/services/user.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,12 @@ export default class UserService {
282282

283283
return raw[0];
284284
}
285+
286+
async getUserLeader(): Promise<User[]> {
287+
return this.repository
288+
.createQueryBuilder('user')
289+
.limit(10)
290+
.orderBy('user.skilvulPoint + user.markodingPoint', 'DESC')
291+
.getMany();
292+
}
285293
}

src/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import fastifyCors from 'fastify-cors';
44
import {bootstrap} from 'fastify-decorators';
55
import fastifyMultipart from 'fastify-multipart';
66

7-
import {IdeaController, LeaderboardController} from './api/controllers/idea';
7+
import {
8+
UserController,
9+
LeaderboardUserController,
10+
} from './api/controllers/user';
11+
import {
12+
IdeaController,
13+
LeaderboardTeamController,
14+
} from './api/controllers/idea';
815
import IdeaUserController from './api/controllers/ideaUser';
916
import IdeaLikeController from './api/controllers/ideaLike';
1017
import IdeaCommentController from './api/controllers/ideaComment';
@@ -20,7 +27,6 @@ import QuestionLikeController from './api/controllers/questionLike';
2027
import ChatController from './api/controllers/chat';
2128
import UploadController from './api/controllers/upload';
2229
import ProfileController from './api/controllers/profile';
23-
import UserController from './api/controllers/user';
2430
import SearchController from './api/controllers/search';
2531

2632
const {APP_PORT = 8080, APP_HOST = '0.0.0.0'} = process.env;
@@ -33,7 +39,8 @@ server.register(fastifyCors, {
3339
});
3440
server.register(bootstrap, {
3541
controllers: [
36-
LeaderboardController,
42+
LeaderboardUserController,
43+
LeaderboardTeamController,
3744
IdeaController,
3845
IdeaUserController,
3946
IdeaLikeController,

0 commit comments

Comments
 (0)