creating conversation with data
This commit is contained in:
@@ -43,6 +43,7 @@ export class ConversationMember {
|
||||
|
||||
createdDate: Timestamp = Timestamp.now();
|
||||
lastUpdatedDate?: Timestamp;
|
||||
lastInteractionDate?: Timestamp;
|
||||
|
||||
constructor(
|
||||
_userId: string,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export enum Collections {
|
||||
enum Collections {
|
||||
users = "users",
|
||||
teams = "teams",
|
||||
teamMembers = "teamMembers",
|
||||
@@ -10,4 +10,10 @@ export enum Collections {
|
||||
links = "links",
|
||||
|
||||
officeRooms = "officeRooms",
|
||||
|
||||
// v2
|
||||
conversations = "conversations",
|
||||
conversationMembers = "members",
|
||||
}
|
||||
|
||||
export default Collections;
|
||||
@@ -0,0 +1,74 @@
|
||||
import {
|
||||
addDoc,
|
||||
collection,
|
||||
doc,
|
||||
Firestore,
|
||||
getFirestore,
|
||||
serverTimestamp,
|
||||
setDoc,
|
||||
runTransaction,
|
||||
writeBatch,
|
||||
} from "firebase/firestore";
|
||||
import Conversation, { ConversationMember } from "../models/conversation";
|
||||
import Collections from "./collections";
|
||||
|
||||
export default class ConversationService {
|
||||
private db: Firestore = getFirestore();
|
||||
|
||||
async test() {
|
||||
const docRef = doc(this.db, "test", "test");
|
||||
await setDoc(
|
||||
docRef,
|
||||
{ hi: " hiii", lastUpdatedDate: serverTimestamp() },
|
||||
{ merge: true }
|
||||
);
|
||||
}
|
||||
|
||||
async createConversation(
|
||||
conversation: Conversation,
|
||||
members: ConversationMember[]
|
||||
) {
|
||||
try {
|
||||
const newConvoRef = doc(
|
||||
this.db,
|
||||
Collections.conversations,
|
||||
conversation.id
|
||||
);
|
||||
|
||||
const newConvo = await runTransaction(this.db, async (transaction) => {
|
||||
transaction.set(
|
||||
newConvoRef,
|
||||
{
|
||||
...conversation,
|
||||
createdDate: serverTimestamp(),
|
||||
},
|
||||
{ merge: true }
|
||||
);
|
||||
|
||||
return newConvoRef;
|
||||
});
|
||||
|
||||
const batch = writeBatch(this.db);
|
||||
|
||||
members.forEach((mem) => {
|
||||
const newMemberRef = doc(
|
||||
this.db,
|
||||
Collections.conversations,
|
||||
newConvo.id,
|
||||
Collections.conversationMembers,
|
||||
mem.id
|
||||
);
|
||||
batch.set(newMemberRef, { ...mem, createdDate: serverTimestamp() });
|
||||
});
|
||||
|
||||
await batch.commit();
|
||||
|
||||
console.log(
|
||||
"submitted batch write for each new member of this conversation"
|
||||
);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new Error("Problem in creating conversation");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import ConversationService from "./conversationService";
|
||||
|
||||
export const conversationService = new ConversationService();
|
||||
Reference in New Issue
Block a user