From 32258475dbdb8a2b835c8a695d8b28979d18e6b1 Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:43:39 +0300 Subject: [PATCH] feat(users): add createdAt property --- src/v3/classes/Congregation.ts | 1 + src/v3/classes/User.ts | 12 ++++++++++++ src/v3/services/firebase/storage_utils.ts | 22 ++++++++++++++++++++++ src/v3/services/firebase/users.ts | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/v3/classes/Congregation.ts b/src/v3/classes/Congregation.ts index d9d9a573..8f226325 100644 --- a/src/v3/classes/Congregation.ts +++ b/src/v3/classes/Congregation.ts @@ -401,6 +401,7 @@ export class Congregation { return { id: member.id, profile: { + createdAt: member.profile.createdAt, global_role: member.profile.role, firstname: member.profile.firstname, lastname: member.profile.lastname, diff --git a/src/v3/classes/User.ts b/src/v3/classes/User.ts index c61dff76..b0e1339a 100644 --- a/src/v3/classes/User.ts +++ b/src/v3/classes/User.ts @@ -15,6 +15,7 @@ import { generateUserSecret } from '../utils/user_utils.js'; import { CongregationsList } from './Congregations.js'; import { saveCongPersons, saveIncomingReports } from '../services/firebase/congregations.js'; import { BackupData } from '../definition/congregation.js'; +import { getFileMetadata } from '../services/firebase/storage_utils.js'; export class User { id: string; @@ -58,6 +59,17 @@ export class User { const data = await getUserAuthDetails(this.profile.auth_uid!); this.email = data.email; this.auth_provider = data.auth_provider; + + if (!this.profile.createdAt) { + this.profile.createdAt = data.createdAt; + } + } + + if (this.profile.role === 'pocket' && !this.profile.createdAt) { + const path = `${this.id}/profile.txt`; + const data = await getFileMetadata({ type: 'user', path }); + + this.profile.createdAt = data?.timeCreated; } this.bible_studies = data.bible_studies; diff --git a/src/v3/services/firebase/storage_utils.ts b/src/v3/services/firebase/storage_utils.ts index ba1342c2..7eba11a2 100644 --- a/src/v3/services/firebase/storage_utils.ts +++ b/src/v3/services/firebase/storage_utils.ts @@ -25,6 +25,27 @@ export const uploadFileToStorage = async (data: string, options: StorageBaseType return encryptedData; }; +export const getFileMetadata = async ({ path, type }: StorageBaseType) => { + let destPath = 'v3/'; + + if (type === 'congregation') { + destPath += `congregations/${path}`; + } + + if (type === 'user') { + destPath += `users/${path}`; + } + + const storageBucket = getStorage().bucket(); + const file = await storageBucket.file(destPath); + + const [fileExist] = await file.exists(); + + if (fileExist) { + return file.metadata; + } +}; + export const getFileFromStorage = async ({ path, type }: StorageBaseType) => { let destPath = 'v3/'; @@ -38,6 +59,7 @@ export const getFileFromStorage = async ({ path, type }: StorageBaseType) => { const storageBucket = getStorage().bucket(); const file = await storageBucket.file(destPath); + const [fileExist] = await file.exists(); if (fileExist) { diff --git a/src/v3/services/firebase/users.ts b/src/v3/services/firebase/users.ts index a4158ab7..f52574cc 100644 --- a/src/v3/services/firebase/users.ts +++ b/src/v3/services/firebase/users.ts @@ -11,7 +11,7 @@ export const getUserAuthDetails = async (auth_uid: string) => { const auth_provider = userRecord.providerData[0]?.providerId || 'email'; - return { email: userRecord.email, auth_provider }; + return { email: userRecord.email, auth_provider, createdAt: userRecord.metadata.creationTime }; }; export const getUsersID = async () => {