Skip to content

Commit

Permalink
feat(users): add createdAt property
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 10, 2024
1 parent a0756bf commit 3225847
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/v3/classes/Congregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/v3/classes/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions src/v3/services/firebase/storage_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/v3/services/firebase/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 3225847

Please sign in to comment.