-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(controllers): move controllers logic to separate services
- Loading branch information
Showing
8 changed files
with
140 additions
and
103 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,17 @@ | ||
import { firestore } from "@/config"; | ||
import { Controller, Subject } from "@/types"; | ||
import * as subjectService from "@/services/subject.service"; | ||
import { Controller } from "@/types"; | ||
import { logger } from "@middleware/logging.middleware"; | ||
|
||
export const getAllSubjects: Controller = async (_req, res) => { | ||
try { | ||
const subjectsRef = firestore.collection("subjects"); | ||
const snapshot = await subjectsRef.get(); | ||
|
||
const subjects = snapshot.docs.map((doc) => { | ||
return { | ||
id: doc.id, | ||
name: doc.data().name, | ||
iconUrl: doc.data().iconUrl, | ||
}; | ||
}); | ||
const subjects = await subjectService.getAllSubjects(); | ||
|
||
res.json({ status: "success", data: subjects }); | ||
} catch (error) { | ||
logger.error(`Error when getting all subjects: ${error}`); | ||
res.status(400).json({ | ||
status: "fail", | ||
message: "Failed to get all subjects.", | ||
message: "Failed to get all subjects", | ||
}); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import sharp from "sharp"; | ||
|
||
// Downscale image to 1024x1024 and convert to JPEG | ||
export const downscaleImage = async (buffer: Buffer) => { | ||
return sharp(buffer).resize(1024, 1024).jpeg({ quality: 80 }).toBuffer(); | ||
}; |
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,16 @@ | ||
import { auth, firestore } from "@/config"; | ||
|
||
export const registerUser = async ( | ||
name: string, | ||
email: string, | ||
password: string, | ||
) => { | ||
const user = await auth.createUser({ displayName: name, email, password }); | ||
|
||
await firestore.collection("users").doc(user.uid).set({ | ||
name, | ||
createdAt: new Date(), | ||
}); | ||
|
||
return { userId: user.uid }; | ||
}; |
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,20 @@ | ||
import { firestore } from "@/config"; | ||
|
||
export const getAllSubjects = async () => { | ||
try { | ||
const subjectsRef = firestore.collection("subjects"); | ||
const snapshot = await subjectsRef.get(); | ||
|
||
const subjects = snapshot.docs.map((doc) => { | ||
return { | ||
id: doc.id, | ||
name: doc.data().name, | ||
iconUrl: doc.data().iconUrl, | ||
}; | ||
}); | ||
|
||
return subjects; | ||
} catch (error) { | ||
throw new Error(`Error when getting all subjects: ${error}`); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
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,59 @@ | ||
import { firestore, GCS_BUCKET_NAME } from "@/config"; | ||
import { downscaleImage } from "@/helpers/image.helper"; | ||
import { Storage } from "@google-cloud/storage"; | ||
import { logger } from "@middleware/logging.middleware"; | ||
import { updateProfileSchema } from "@schemas/user.schema"; | ||
import firebase from "firebase-admin"; | ||
import { z } from "zod"; | ||
|
||
const storage = new Storage(); | ||
|
||
export const updateUserProfile = async ( | ||
userId: string, | ||
data: z.infer<typeof updateProfileSchema>["body"], | ||
) => { | ||
const { location, ...restOfData } = data; | ||
|
||
// Handle location separately if present | ||
const newData = location | ||
? { | ||
...restOfData, | ||
location: new firebase.firestore.GeoPoint( | ||
location.latitude, | ||
location.longitude, | ||
), | ||
} | ||
: restOfData; | ||
|
||
try { | ||
await firestore | ||
.collection("users") | ||
.doc(userId) | ||
.update({ | ||
...newData, | ||
updatedAt: new Date(), | ||
}); | ||
} catch (error) { | ||
throw new Error("Failed to update profile"); | ||
} | ||
}; | ||
|
||
// Upload to: profile-pictures/{uid}.jpg | ||
export const updateUserProfilePicture = async ( | ||
file: Express.Multer.File, | ||
userId: string, | ||
) => { | ||
try { | ||
const image = await downscaleImage(file.buffer); | ||
const bucket = storage.bucket(GCS_BUCKET_NAME); | ||
const bucketFile = bucket.file(`profile-pictures/${userId}.jpg`); | ||
|
||
await bucketFile.save(image, { public: true }); | ||
|
||
return `https://storage.googleapis.com/${GCS_BUCKET_NAME}/${bucketFile.name}`; | ||
} catch (error) { | ||
logger.error(`Failed to upload profile picture: ${error}`); | ||
|
||
return undefined; | ||
} | ||
}; |