SwapIt / utils / user.mjs
user.mjs
Raw
import {schema} from "./database.mjs";

async function Operations(){
    //Read Operation
    async function GetNewUserID(){
        const uid = await schema.googleSheets.spreadsheets.values.get({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: "ID",
        });
        let data = uid.data.values
        const user = data[1][0];
        let n = parseInt(user) +1;
        data[1][0] = n;
        await schema.googleSheets.spreadsheets.values.update({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: 'ID',
            valueInputOption: 'USER_ENTERED',
            resource : {
                values : data
            }
        })
        return user;
    }
    async function GetNewProductID(){
        const pid = await schema.googleSheets.spreadsheets.values.get({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: "ID",
        });
        let data = pid.data.values
        const product = data[1][1];
        let n = parseInt(product) +1;
        data[1][1] = n;
        await schema.googleSheets.spreadsheets.values.update({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: 'ID',
            valueInputOption: 'USER_ENTERED',
            resource : {
                values : data
            }
        })
        return product;
    }

    async function ReadUser(){
        const ud = await schema.googleSheets.spreadsheets.values.get({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: "Users_Detail",
        });
        return ud.data.values;
    }
    async function ReadProduct(){
        const pd = await schema.googleSheets.spreadsheets.values.get({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: "Products_Detail",
        });
       return pd.data.values;
    }
    // const Products_Detail = ReadProduct();

    async function CreateUser(Name,Email,Mobile,AltMobile="null",Ig="null",Image = "null",date=Date().toString()){
        const Next_User_Id = await GetNewUserID();
        await schema.googleSheets.spreadsheets.values.append({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: "Users_Detail",
            valueInputOption: "USER_ENTERED",
            resource: {
                values : [[Next_User_Id,Name,Email,Mobile,AltMobile,Ig,Image,date=Date().toString()]]
            }
        })
    }
    async function CreateProduct(User_Id, NOP, type, image, desc, price, date){
        const Next_Product_Id = await GetNewProductID();
        await schema.googleSheets.spreadsheets.values.append({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: "Products_Detail",
            valueInputOption: "USER_ENTERED",
            resource: {
                values : [[Next_Product_Id,User_Id, NOP, type, image, desc, price, date]]
            }
        })
    }
    async function findEmail(email){
        const Users_Detail = await ReadUser();
        for(let i=1; i<Users_Detail.length;i++){
            if(email === Users_Detail[i][2]) return { found: true, data: Users_Detail[i]};
        }
        return {found : false, data: null};
    }

    async function findUserById(Id){
        const Users_Detail = await ReadUser();
        for(let i=1; i<Users_Detail.length;i++){
            if(Id === Users_Detail[i][0]) return { found: true, data: Users_Detail[i]};
        }
        return {found : false, data: null};
    }

    async function findProductById(Id){
        const Products_Detail = await ReadProduct();
        for(let i=1; i<=Products_Detail.length;i++){
            if(Id === Products_Detail[i][0]) return { found: true, data: Products_Detail[i]};
        }
        return {found : false, data: null};
    }

    async function updateProductById(updatedProduct,Product_ID){
        const Products_Detail = await ReadProduct();
        let index = -1 ;
        Products_Detail.map((product) => {
            if(product[0] == Product_ID) index = Products_Detail.indexOf(product) 
        })
        if(index!=-1) Products_Detail[index] = updatedProduct;
        // console.log(Products_Detail)
        await schema.googleSheets.spreadsheets.values.clear({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: 'Products_Detail'
        })
        await schema.googleSheets.spreadsheets.values.update({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: 'Products_Detail',
            valueInputOption: 'USER_ENTERED',
            resource : {
                values : Products_Detail
            }
        });
    }
    async function DeleteProductById(Product_ID){
        const Products_Detail = await ReadProduct();
        const newData = Products_Detail.filter((product) => product[0]!=Product_ID);
        // console.log(newData);
        await schema.googleSheets.spreadsheets.values.clear({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: 'Products_Detail'
        })
        await schema.googleSheets.spreadsheets.values.update({
            auth: schema.auth,
            spreadsheetId: schema.spreadsheetId,
            range: 'Products_Detail',
            valueInputOption: 'USER_ENTERED',
            resource : {
                values : newData
            }
        });
    }

    return {ReadProduct,ReadUser,CreateUser,CreateProduct,findEmail, findProductById, findUserById , updateProductById, DeleteProductById};
}
export const op = await Operations();

// op.CreateProduct("1","hello","select", , "dwdaada", "100")