Skip to content

Commit

Permalink
[ENGG-1646] feat: collection support for mocks (#1634)
Browse files Browse the repository at this point in the history
* add MockCollectionModal

* chore: update types

* add `createMockCollection` controller

* add create new collection cta

* add collection modal on mocks listing

* fix: fetch mocks

* chore: mock metadata

* render collections

* add `updateMockCollection` controller

* rename

* add update bulk mock collection id controller

* add delete mocks controller

* cleanup

* fix: delete mock

* add deleteMockCollection

* add deleteMocks

* add update and delete collection modals

* fix

* add row actions

* fix: toggle modal

* rename createCollection

* rename deleteCollectionModal

* rename createCollectionModal

* rename

* fix: bulk update collections

* add comment

* refactor

* add update mock collection modal

* fix: row expand

* chore: add comment

* fix: style

* refactor: delete mock modal

* feat: add star mock suuport

* fix: style

* chore: fix type

* add table filters

* fix: all filter

* chore: update analytics

* [ENGG-1686] refactor: mocks feature (#1648)

* fix: mock picker modal

* add CreateCollectionModalWrapper

* refactor: delete modal

* refactor: delete mock modal

* rebase

* fix: force re-renders

* update mock collection modal

* cleanup

* fix: import

* fix: force render

* cleanup

* fix: empty actions

* refactor: mock uploader modal

* refactor: new file modal

* fix: mock name overflow

* fix: star option

* cleanup

* fix: create mock from picker modal

* add comment

* fix: toast

* address review comments

* refactor

* fix: modal header

* refactor

* refactor

* fix: delete collection

* fix: select option

* fix: style

* fix: style

* refactor

* refactor: mocks -> mockRecords

* fix: mock description

* add remove mock from collection action

* fix: delete collection

* fix: created by

* refactor

* fix: search

* fix: deps

* fix: filters

* fix: actions

* fix: style

* fix: typo

* fix: collection creation

* fix: row style

* fix: action order

* cleanup

* refactor

* fix: collection selection

* chore: add comment

* refactor

* refactor

* refactor: delete mocks

* chore: fix types

* refactor: for bulk onsuccess

* fix: style

Signed-off-by: rohanmathur91 <mathurrohan04@gmail.com>

* add bulk actions

Signed-off-by: rohanmathur91 <mathurrohan04@gmail.com>

* fix: style

* fix: style

* fix: rerender issue

* fix: collection order

* fix: onSuccess

* fix: style

* fix: style

* fix: style

* fix: style

* fix: label

* fix: mock actions

* fix: action

* refactor

* cleanup

* refactor: updateMocksCollectionAction

* refactor: uploadMockAction

* refactor: createNewFileAction

* fix: typo

* fix: typo

* refactor: delete records action

* refactor

* refactor

---------

Signed-off-by: rohanmathur91 <mathurrohan04@gmail.com>
  • Loading branch information
rohanmathur91 committed May 13, 2024
1 parent 7ff78a0 commit d1ddf13
Show file tree
Hide file tree
Showing 70 changed files with 2,527 additions and 2,098 deletions.
55 changes: 55 additions & 0 deletions app/src/backend/mocks/createCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import firebaseApp from "../../firebase";
import { getFirestore, Timestamp, updateDoc, addDoc, collection } from "firebase/firestore";
import { getOwnerId } from "backend/utils";
import Logger from "lib/logger";
import { MockRecordType, RQMockCollection } from "components/features/mocksV2/types";

type MockCollectionData = Pick<RQMockCollection, "name" | "desc" | "type">;

export const createCollection = async (
uid: string,
mockCollectionData: MockCollectionData,
teamId?: string
): Promise<RQMockCollection | null> => {
if (!uid) {
return null;
}

const mockCollection = await createCollectionInFirebase(uid, mockCollectionData, teamId);

return mockCollection;
};

const createCollectionInFirebase = async (
uid: string,
mockCollectionData: MockCollectionData,
teamId?: string
): Promise<RQMockCollection | null> => {
const db = getFirestore(firebaseApp);
const mocksRef = collection(db, "mocks");
const ownerId = getOwnerId(uid, teamId);

try {
const collectionData: RQMockCollection = {
...mockCollectionData,
recordType: MockRecordType.COLLECTION,
ownerId: ownerId,
deleted: false,
createdBy: uid,
lastUpdatedBy: uid,
createdTs: Timestamp.now().toMillis(),
updatedTs: Timestamp.now().toMillis(),
};

const docRef = await addDoc(mocksRef, collectionData);

Logger.log(`Mock collection document created ${docRef.id}`);

await updateDoc(docRef, { id: docRef.id });

return { ...collectionData, id: docRef.id } as RQMockCollection;
} catch (err) {
Logger.error("Error while creating mock collection", err);
return null;
}
};
4 changes: 3 additions & 1 deletion app/src/backend/mocks/createMock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import firebaseApp from "../../firebase";
import { getFirestore, Timestamp, updateDoc, addDoc, collection } from "firebase/firestore";
import { RQMockSchema } from "components/features/mocksV2/types";
import { MockRecordType, RQMockSchema } from "components/features/mocksV2/types";
import { getOwnerId } from "backend/utils";
import { updateUserMockSelectorsMap, uploadResponseBodyFiles } from "./common";
import { BODY_IN_BUCKET_ENABLED } from "./constants";
Expand Down Expand Up @@ -48,7 +48,9 @@ const createMockFromFirebase = async (uid: string, mockData: RQMockSchema, teamI
const ownerId = getOwnerId(uid, teamId);

const mockId: string | null = await addDoc(rootMocksCollectionRef, {
collectionId: "",
...mockData,
recordType: MockRecordType.MOCK,
createdBy: uid,
ownerId: ownerId,
deleted: false,
Expand Down
1 change: 1 addition & 0 deletions app/src/backend/mocks/deleteMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const deleteMockFromFirebase = async (mockId: string): Promise<boolean> => {

const success = await updateDoc(docRef, {
deleted: true,
collectionId: "",
updatedTs: Timestamp.now().toMillis(),
})
.then(() => {
Expand Down
47 changes: 47 additions & 0 deletions app/src/backend/mocks/deleteMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import firebaseApp from "../../firebase";
import { doc, getFirestore, Timestamp, writeBatch, collection, deleteField } from "firebase/firestore";
import { getOwnerId } from "backend/utils";

export const deleteMocks = async (uid: string, mockIds: string[], teamId?: string): Promise<boolean> => {
if (!uid) {
return false;
}

const ownerId = getOwnerId(uid, teamId);
const result = await deleteMocksFromFirebase(uid, ownerId, mockIds);
return result;
};

const deleteMocksFromFirebase = async (uid: string, ownerId: string, mockIds: string[]): Promise<boolean> => {
try {
const db = getFirestore(firebaseApp);
const mocksbatch = writeBatch(db);
const userMocksMetadataRef = collection(db, "user-mocks-metadata");
const userDocRef = doc(userMocksMetadataRef, ownerId);

mockIds.forEach((mockId) => {
const mockRef = doc(db, "mocks", mockId);

mocksbatch.set(
mockRef,
{
deleted: true,
collectionId: "",
lastUpdatedBy: uid,
updatedTs: Timestamp.now().toMillis(),
},
{ merge: true }
);

mocksbatch.update(userDocRef, {
[`mockSelectors.${mockId}`]: deleteField(),
});
});

await mocksbatch.commit();

return true;
} catch (error) {
return false;
}
};
36 changes: 36 additions & 0 deletions app/src/backend/mocks/updateCollections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import firebaseApp from "../../firebase";
import { doc, getFirestore, Timestamp, writeBatch } from "firebase/firestore";
import { RQMockCollection } from "components/features/mocksV2/types";

export const updateCollections = async (uid: string, collections: Partial<RQMockCollection>[]): Promise<boolean> => {
if (!uid) {
return null;
}

const result = await updateCollectionsInFirebase(uid, collections);

return result;
};

const updateCollectionsInFirebase = async (uid: string, collections: Partial<RQMockCollection>[]): Promise<boolean> => {
try {
const db = getFirestore(firebaseApp);
const collectionsBatch = writeBatch(db);

collections.forEach((collection) => {
const collectionRef = doc(db, "mocks", collection.id);

collectionsBatch.set(
collectionRef,
{ ...collection, lastUpdatedBy: uid, updatedTs: Timestamp.now().toMillis() },
{ merge: true }
);
});

await collectionsBatch.commit();

return true;
} catch (error) {
return false;
}
};
3 changes: 2 additions & 1 deletion app/src/backend/mocks/updateMock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import firebaseApp from "../../firebase";
import { doc, getFirestore, Timestamp, updateDoc } from "firebase/firestore";
import { RQMockSchema } from "components/features/mocksV2/types";
import { MockRecordType, RQMockSchema } from "components/features/mocksV2/types";
import { getOwnerId } from "backend/utils";
import { updateUserMockSelectorsMap, uploadResponseBodyFiles } from "./common";
import { BODY_IN_BUCKET_ENABLED } from "./constants";
Expand Down Expand Up @@ -53,6 +53,7 @@ export const updateMockFromFirebase = async (
const success = await updateDoc(docRef, {
lastUpdatedBy: updaterId,
...mockData,
recordType: MockRecordType.MOCK,
updatedTs: Timestamp.now().toMillis(),
})
.then(() => {
Expand Down
41 changes: 41 additions & 0 deletions app/src/backend/mocks/updateMocksCollectionId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import firebaseApp from "../../firebase";
import { Timestamp, doc, getFirestore, writeBatch } from "firebase/firestore";

export const updateMocksCollectionId = async (
uid: string,
mockIds: string[],
updatedCollectionId: string
): Promise<boolean> => {
if (!uid) {
return null;
}

const result = await updateMocksCollectionIdInFirebase(uid, mockIds, updatedCollectionId);
return result;
};

const updateMocksCollectionIdInFirebase = async (
uid: string,
mockIds: string[],
updatedCollectionId: string
): Promise<boolean> => {
try {
const db = getFirestore(firebaseApp);
const mocksbatch = writeBatch(db);

mockIds.forEach((id) => {
const mockRef = doc(db, "mocks", id);
mocksbatch.set(
mockRef,
{ collectionId: updatedCollectionId, lastUpdatedBy: uid, updatedTs: Timestamp.now().toMillis() },
{ merge: true }
);
});

await mocksbatch.commit();

return true;
} catch (error) {
return false;
}
};
102 changes: 0 additions & 102 deletions app/src/components/features/mocksV2/DeleteMockModal/index.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const MockEditorIndex: React.FC<Props> = ({
setSavingInProgress(false);
if (success) {
toast.success("Mock Updated Successfully");
trackUpdateMockEvent(mockId, mockType, finalMockData?.fileType);
trackUpdateMockEvent(mockId, mockType, finalMockData?.fileType, finalMockData?.collectionId);
return setMockEditorData(data);
}
toast.error("Mock Update Error");
Expand Down
Loading

0 comments on commit d1ddf13

Please sign in to comment.