Skip to content

Commit

Permalink
[done] feat/server-user
Browse files Browse the repository at this point in the history
  • Loading branch information
FahimMontasir committed Sep 17, 2023
1 parent 34386e0 commit 43b709e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
10 changes: 8 additions & 2 deletions backend/src/modules/rest/auth/app/auth.app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import configs from 'configs';
import { IDecodedUser, IUserRoles } from 'interfaces/user';
import { Secret } from 'jsonwebtoken';
import { pick } from 'lodash';
import { NOTIFICATION_TOPIC } from 'shared/pushNotification';
import { NOTIFICATION_TOPIC, PushNotification } from 'shared/pushNotification';

const login = async (
body: IReq
Expand Down Expand Up @@ -54,7 +54,13 @@ const login = async (
if (userExists) {
const checkFcm = userExists.fcmTokens.filter(v => v.device !== fcmToken.device);
if (checkFcm.length) {
//Todo: send notification to existing device
// send notification to existing device
PushNotification.send(checkFcm, {
type: 'app-update',
title: 'New device login!',
body: `Device Name: ${fcmToken.device}`,
data: { screenId: 'setting-modal' },
});
}

const fcmTokens = [...checkFcm, fcmToken];
Expand Down
11 changes: 11 additions & 0 deletions backend/src/modules/rest/user/app/user.app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { sendResponse } from 'shared/sendResponse';
import { AppUserService } from './user.app.service';

// -------------------------------get-------------------------------
const getUpdatedInfo = catchAsync(async (req, res) => {
const data = await AppUserService.getUpdatedInfo(req.user);

sendResponse(res, {
statusCode: 200,
success: true,
message: 'Retrieved user updated info successfully!!!',
data,
});
});

// ----------------------------------add----------------------------
const refreshFcmToken = catchAsync(async (req, res) => {
Expand Down Expand Up @@ -58,6 +68,7 @@ const removeOtherUser = catchAsync(async (req, res) => {
});

export const AppUserController = {
getUpdatedInfo,
refreshFcmToken,
addSkill,
updateUser,
Expand Down
3 changes: 2 additions & 1 deletion backend/src/modules/rest/user/app/user.app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { AppUserController } from './user.app.controller';

const router = express.Router();

router.get('/updated-info', auth('user', 'userN'), AppUserController.getUpdatedInfo);

router.post(
'/refresh-fcm-token',
auth('user', 'userN'),
Expand All @@ -32,7 +34,6 @@ router.delete(
validateRequest(AppUserValidation.removeSkillZodSchema),
AppUserController.removeSkill
);

router.delete(
'/remove-other-user',
auth('user', 'userN'),
Expand Down
43 changes: 43 additions & 0 deletions backend/src/modules/rest/user/app/user.app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,49 @@ import { NOTIFICATION_TOPIC } from 'shared/pushNotification';
import ApiError from 'errors/ApiError';

// ---------------get----------------------
const getUpdatedInfo = async (decodedUser: IDecodedUser) => {
const { _id } = decodedUser;

const userData = await AppUser.findById(_id)
.select('-_id fcmTokens fullName imageUrl coverUrl interests badges upVotes downVotes isBanned')
.lean();
// todo: remove this dummy data with proper data
const materialSections = [
{
title: 'English basic for beginner',
thumbnail:
'https://t3.ftcdn.net/jpg/03/70/42/66/360_F_370426690_Pejt9KxjWTHPklsKwripaxr0iA17zupF.jpg',
},
{
title: 'English for web developer',
thumbnail:
'https://www.fluentu.com/blog/english/wp-content/uploads/sites/4/2022/08/how-to-speak-english-fluently-1024x683.jpg',
},
{
title: 'Advance English grammar',
thumbnail:
'https://www.21kschool.com/blog/wp-content/uploads/2022/10/The-Importance-of-Grammar-in-Learning-the-English-Language.png',
},
{
title: 'English for daily conversation',
thumbnail:
'https://t3.ftcdn.net/jpg/03/70/42/66/360_F_370426690_Pejt9KxjWTHPklsKwripaxr0iA17zupF.jpg',
},
{
title: 'English for Business Communication',
thumbnail:
'https://www.fluentu.com/blog/english/wp-content/uploads/sites/4/2022/08/how-to-speak-english-fluently-1024x683.jpg',
},
];

return {
userData: {
...userData,
fcmTokens: userData?.fcmTokens.map(t => ({ token: t.token, device: t.device })),
},
materialSections,
};
};
// -------------------add------------------
const refreshFcmToken = async (decodedUser: IDecodedUser, fcmToken: IFcmToken): Promise<void> => {
const { _id } = decodedUser;
Expand Down Expand Up @@ -71,6 +113,7 @@ const removeOtherUser = async (decodedUser: IDecodedUser, device: string): Promi
};

export const AppUserService = {
getUpdatedInfo,
refreshFcmToken,
addSkill,
updateUser,
Expand Down

0 comments on commit 43b709e

Please sign in to comment.