Skip to content

Commit

Permalink
BUG Fixes for save
Browse files Browse the repository at this point in the history
  • Loading branch information
loeiks committed Jul 30, 2024
1 parent 4de49f8 commit d9f5141
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ In this file you can find what's changed in each version. (Versions with -dev, -

---

### 4.9.3^

- BUG Fixes for some functions.
- Security updates/fixes.

### 4.9.1

- BUG fix for query and aggregation filters (weivData.filters).
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exweiv/weiv-data",
"version": "4.9.1",
"version": "4.9.4",
"description": "Custom API Library for Wix sites to connect MongoDB. Designed to easily switch from wix-data APIs.",
"main": "./lib/index.js",
"files": [
Expand Down
28 changes: 15 additions & 13 deletions app/src/Functions/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
const context = prepareHookContext(collectionId);
const { suppressAuth, suppressHooks, readConcern, onlyOwner, enableVisitorId, convertIds } = { convertIds: getConvertIdsValue(), ...safeOptions };

// Convert ID to ObjectId if exist
let actionType: "insert" | "update" = "insert";
let editedItem;

if (safeItem._id) {
// Update
safeItem._id = convertIdToObjectId(safeItem._id);
actionType = "update";

if (suppressHooks != true) {
editedItem = await runDataHook<'beforeUpdate'>(collectionId, "beforeUpdate", [safeItem, context]).catch((err) => {
Expand All @@ -32,6 +34,7 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
} else {
// Insert
safeItem._owner = await getOwnerId(enableVisitorId);
actionType = "insert";

if (suppressHooks != true) {
editedItem = await runDataHook<'beforeInsert'>(collectionId, "beforeInsert", [safeItem, context]).catch((err) => {
Expand All @@ -46,27 +49,24 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
}

// For updates
let filter: { _id: ObjectId, _owner?: string } | undefined;
if (safeItem._id && typeof safeItem._id === "string" && onlyOwner) {
filter = { _id: editedItem._id };
const filter: { _id: ObjectId, _owner?: string } = safeItem._id ? { _id: safeItem._id } : { _id: new ObjectId() };
if (onlyOwner) {
const currentMemberId = await getOwnerId(enableVisitorId);
if (currentMemberId) {
filter._owner = currentMemberId;
}
}

const { collection } = await connectionHandler(collectionId, suppressAuth);
const { upsertedId, acknowledged } = await collection.updateOne(
filter ? filter : { _id: new ObjectId() },
const returnedItem = await collection.findOneAndUpdate(
filter,
{ $set: { ...editedItem, _updatedDate: new Date() }, $setOnInsert: !editedItem._createdDate ? { _createdDate: new Date() } : {} },
{ readConcern, upsert: true }
{ readConcern, upsert: true, returnDocument: "after" }
);

const returnedItem = { ...editedItem, _id: editedItem._id };

if (acknowledged) {
if (returnedItem) {
// Hooks handling
if (upsertedId) {
if (actionType === "insert") {
// Item Inserted
const editedResult = await runDataHook<'afterInsert'>(collectionId, "afterInsert", [convertIds ? convertDocumentIDs(returnedItem) : returnedItem, context]).catch((err) => {
kaptanLogar("00003", `afterInsert Hook Failure ${err}`);
Expand All @@ -77,7 +77,7 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
} else {
return convertIds ? { item: convertDocumentIDs(returnedItem) } : { item: returnedItem };
}
} else {
} else if (actionType === "update") {
// Item Updated
const editedResult = await runDataHook<'afterUpdate'>(collectionId, "afterUpdate", [convertIds ? convertDocumentIDs(returnedItem) : returnedItem, context]).catch((err) => {
kaptanLogar("00003", `afterUpdate Hook Failure ${err}`);
Expand All @@ -88,9 +88,11 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
} else {
return convertIds ? { item: convertDocumentIDs(returnedItem) } : { item: returnedItem };
}
} else {
kaptanLogar("00016", `this error is not expected, try again or create a issue in WeivData GitHub repo`);
}
} else {
kaptanLogar("00016", `acknowledged is not true for (save function)`);
kaptanLogar("00016", `couldn't save item, this error is unexpected`);
}
} catch (err) {
kaptanLogar("00016", `when saving an item to collection: ${err}`);
Expand Down

0 comments on commit d9f5141

Please sign in to comment.