-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
275 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function sleep(ms: number): Promise<void> { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Sync eCom Orders to MongoDB with CUD Operations | ||
import type { Document } from "mongodb"; | ||
import { kaptanLogar } from "../Errors/error_manager"; | ||
import { insert } from "../Functions/insert"; | ||
import { native } from "../Functions/native"; | ||
import { sleep } from "./sleep"; | ||
|
||
//@ts-ignore | ||
import wixData from 'wix-data'; | ||
import { getWeivDataConfigs } from "../Config/weiv_data_config"; | ||
|
||
export async function onMemberCreated(event: Document): Promise<void> { | ||
try { | ||
// Wait 1s before inserting (in case of data not inserted in Wix app collection yet) | ||
await sleep(1000); | ||
|
||
// Get required information | ||
const memberId = event.entity._id; | ||
const { syncDatabase } = getWeivDataConfigs(); | ||
|
||
if (!syncDatabase) { | ||
kaptanLogar("00024", "You didn't configure any database name to sync Wix apps data!"); | ||
} | ||
|
||
const { readyFullData, readyPrivateData, readyPublicData } = await getMemberData(memberId); | ||
|
||
// Insert to MongoDB (fire and forget) | ||
Promise.all([ | ||
(await native(`${syncDatabase}/WixMembersPublicData`, true)).insertOne(readyPublicData, { retryWrites: true }), | ||
(await native(`${syncDatabase}/WixMembersPrivateData`, true)).insertOne(readyPrivateData, { retryWrites: true }), | ||
(await native(`${syncDatabase}/WixMembersFullData`, true)).insertOne(readyFullData, { retryWrites: true }), | ||
]); | ||
} catch (err) { | ||
// Log Error (fire and forget) | ||
insert("WeivDataWixAppsSyncLogs/WixMembers", { | ||
message: "Member couldn't be created", | ||
entityId: event.entity._id, | ||
metadata: event.metadata | ||
}, { suppressAuth: true, suppressHooks: true }); | ||
|
||
// Throw Error | ||
kaptanLogar("00024", `Couldn't insert member when syncing: ${err}`); | ||
} | ||
}; | ||
|
||
export async function onMemberUpdated(event: Document): Promise<void> { | ||
try { | ||
// Wait 1s before update (in case of data not updated in Wix app collection yet) | ||
await sleep(1000); | ||
|
||
// Get required information | ||
const memberId = event.entity._id; | ||
const { syncDatabase } = getWeivDataConfigs(); | ||
|
||
if (!syncDatabase) { | ||
kaptanLogar("00024", "You didn't configure any database name to sync Wix apps data!"); | ||
} | ||
|
||
const { readyFullData, readyPrivateData, readyPublicData } = await getMemberData(memberId); | ||
const find = { "entityId": memberId }; | ||
|
||
// Insert to MongoDB (fire and forget) | ||
Promise.all([ | ||
(await native(`${syncDatabase}/WixMembersPublicData`, true)).updateOne(find, readyPublicData, { retryWrites: true }), | ||
(await native(`${syncDatabase}/WixMembersPrivateData`, true)).updateOne(find, readyPrivateData, { retryWrites: true }), | ||
(await native(`${syncDatabase}/WixMembersFullData`, true)).updateOne(find, readyFullData, { retryWrites: true }), | ||
]); | ||
} catch (err) { | ||
// Log Error (fire and forget) | ||
insert("WeivDataWixAppsSyncLogs/WixMembers", { | ||
message: "Member couldn't be updated", | ||
entityId: event.entity._id, | ||
metadata: event.metadata | ||
}, { suppressAuth: true, suppressHooks: true }); | ||
|
||
// Throw Error | ||
kaptanLogar("00024", `Couldn't update member when syncing: ${err}`); | ||
} | ||
}; | ||
|
||
export async function onMemberDeleted(event: Document): Promise<void> { | ||
try { | ||
// Get required information | ||
const memberId = event.metadata.entityId; | ||
const { syncDatabase } = getWeivDataConfigs(); | ||
|
||
if (!syncDatabase) { | ||
kaptanLogar("00024", "You didn't configure any database name to sync Wix apps data!"); | ||
} | ||
|
||
const find = { "entityId": memberId }; | ||
|
||
// Insert to MongoDB (fire and forget) | ||
Promise.all([ | ||
(await native(`${syncDatabase}/WixMembersPublicData`, true)).deleteMany(find, { retryWrites: true }), | ||
(await native(`${syncDatabase}/WixMembersPrivateData`, true)).deleteMany(find, { retryWrites: true }), | ||
(await native(`${syncDatabase}/WixMembersFullData`, true)).deleteMany(find, { retryWrites: true }), | ||
]); | ||
} catch (err) { | ||
// Log Error (fire and forget) | ||
insert("WeivDataWixAppsSyncLogs/WixMembers", { | ||
message: "Member couldn't deleted", | ||
entityId: event.metadata.entityId, | ||
metadata: event.metadata | ||
}, { suppressAuth: true, suppressHooks: true }); | ||
|
||
// Throw Error | ||
kaptanLogar("00024", `Couldn't update member when syncing: ${err}`); | ||
} | ||
}; | ||
|
||
// HELPER FUNCTIONS | ||
type SyncMemberData = { | ||
readyPublicData: Document, | ||
readyPrivateData: Document, | ||
readyFullData: Document | ||
} | ||
|
||
async function getMemberData(memberId: string): Promise<SyncMemberData> { | ||
try { | ||
if (!memberId) { | ||
kaptanLogar("00024", "Member ID is undefined when syncing WixMembers"); | ||
} | ||
|
||
const [publicData, privateData, fullData] = await Promise.all([ | ||
wixData.get("Members/PublicData", memberId, { suppressAuth: true, suppressHooks: true }), | ||
wixData.get("Members/PrivateMembersData", memberId, { suppressAuth: true, suppressHooks: true }), | ||
wixData.get("Members/FullData", memberId, { suppressAuth: true, suppressHooks: true }), | ||
]); | ||
|
||
const readyPublicData = { ...publicData, entityId: publicData._id }; | ||
delete readyPublicData._id; | ||
|
||
const readyPrivateData = { ...privateData, entityId: privateData._id, }; | ||
delete readyPrivateData._id; | ||
|
||
const readyFullData = { ...fullData, entityId: fullData._id, }; | ||
delete readyFullData._id; | ||
|
||
return { | ||
readyPublicData, | ||
readyPrivateData, | ||
readyFullData | ||
} | ||
} catch (err) { | ||
kaptanLogar("00024", `Couldn't get member data when syncing: ${err}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
// //@ts-ignore | ||
// import * as weivDataConfigs from '../../../../../../../../../user-code/backend/WeivData/config'; | ||
// import type { CustomOptions } from '@exweiv/weiv-data'; | ||
// import { kaptanLogar } from '../Errors/error_manager'; | ||
//@ts-ignore | ||
import * as weivDataConfigs from '../../../../../../../../../user-code/backend/WeivData/config'; | ||
import type { CustomOptions } from '@exweiv/weiv-data'; | ||
import { kaptanLogar } from '../Errors/error_manager'; | ||
|
||
// var __weivDatasavedConfigs__: CustomOptions.WeivDataConfig = { | ||
// _idType: "ObjectID" | ||
// }; | ||
var __weivDatasavedConfigs__: CustomOptions.WeivDataConfig = {}; | ||
|
||
// export function getWeivDataConfigs(): CustomOptions.WeivDataConfig { | ||
// try { | ||
// const configs: undefined | (() => CustomOptions.WeivDataConfig) = weivDataConfigs["config"]; | ||
export function getWeivDataConfigs(): CustomOptions.WeivDataConfig { | ||
try { | ||
const configs: undefined | (() => CustomOptions.WeivDataConfig) = weivDataConfigs["config"]; | ||
|
||
// if (configs && Object.keys(__weivDatasavedConfigs__).length === 0) { | ||
// const userConfig = configs(); | ||
// __weivDatasavedConfigs__ = { ...__weivDatasavedConfigs__, ...userConfig }; | ||
// } | ||
if (configs && Object.keys(__weivDatasavedConfigs__).length === 0) { | ||
const userConfig = configs(); | ||
__weivDatasavedConfigs__ = { ...__weivDatasavedConfigs__, ...userConfig }; | ||
} | ||
|
||
// return __weivDatasavedConfigs__; | ||
// } catch (err) { | ||
// kaptanLogar("00021", `while getting configs of WeivData library, ${err}`); | ||
// } | ||
// } | ||
return __weivDatasavedConfigs__; | ||
} catch (err) { | ||
kaptanLogar("00021", `while getting configs of WeivData library, ${err}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters