@clsnlsen
CODING ISSUE SOLVED - PROCESS WORKING FINE FOR ADMIN USER;
USER RIGHTS QUESTION REMAINING
Hi Claes,
I solved the coding part (see below for the code working fine now), but there is a user rights issue still, as it works only in the editor view, but not in live mode, if the user has no admin rights.
I passed “options” supprAuth etc., but this is apparently not enough, as I am calling the function contained in a backend .jsw-module from the front-end, at log-in.
Everything related to the “source” collection (form submission type) works fine, including backend update. But it does not insert the record into the “target” collection (create: admin, read: admin, update: member (author), delete: admin) supprAuth does not overwrite the users rights. But setting “create” to everybody did not help either, nor setting update to “member” (not author).
It’s about this piece in the code - it does not INSERT the record in the collection (it does in the editor mode).
.then( () => {
wixData.query(target,options)
.eq("_id", item._id)
.find(options)
.then( (conclusion) => {
if(conclusion.items.length === 0) {
wixData.insert(target,item,options);
}
})
I probably will have to link the code to a hook “before insertion” regarding the “target” collection in a backend .js file, do I? But I have no clue, which hook to take … and how to adapt the below code to such a hook .js - and when to call it, and from where.
Could you give advice, please?
Thanks in advance & best regards,
Thomas
For the sake of completeness, see the complete code hereafter. Probably not elegant … but as long as it is stable. …
import wixData from 'wix-data';
import wixUsersBackend from 'wix-users-backend';
export async function updateItem() {
let source = "ABC"
let target = "XYZ"
let item = []
let options = {
"suppressAuth": true,
"suppressHooks": true};
let user = wixUsersBackend.currentUser;
let isLoggedIn = await user.loggedIn;
const email = await user.getEmail(wixUsersBackend,user.id,options)
if (email.length >0){
wixData.query(source,options)
.eq("eMail", email)
.find(options)
.then( (results) => {
if(results.items.length > 0) {
item = results.items[0];
let firstLogin = item.hasLoggedIn
let hit = item._id
if (firstLogin === false) {
wixData.get(source, hit,options)
.then( () => {
item.hasLoggedIn = true;
wixData.update(source,item,options)
return item
} )
.then( () => {
wixData.query(target,options)
.eq("_id", item._id)
.find(options)
.then( (conclusion) => {
if(conclusion.items.length === 0) {
wixData.insert(target,item,options);
}
})
})
} else {
console.log("user boolean already set to true")
}
} else {
console.log("ERROR - user has logged in, but no record found!")
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}}