import Server from "@syncano/core"; export const stawkiKlient = async ( ctx: any, adres: String, restauracjaId: Number, dystans: Number ) => { const { data } = new Server(ctx) const matches = !adres.toLowerCase().match(/białystok/) const restauracja = await data.restauracja .where("id", restauracjaId) .firstOrFail() const strefaRestauracja = await data.strefaRestauracja .where("restauracja", restauracja.user) .list(); const strefy = await data.strefa.list(); let stawkaKlient = strefaRestauracja.find(item => item.strefa === (strefy.find(f=>f.dystansMax === 0).id)); console.log(stawkaKlient) if (matches) { stawkaKlient = strefaRestauracja.find(item => item.strefa === (strefy.find(f=>f.dystansMax === 8000).id)); if (dystans > 8000) { stawkaKlient = strefaRestauracja.find(item => item.strefa === (strefy.find(f=>f.dystansMax === 12000).id)); } if (dystans > 12000) { stawkaKlient = strefaRestauracja.find(item => item.strefa === (strefy.find(f=>f.dystansMax === 16000).id)); } if (dystans > 16000) { stawkaKlient = strefaRestauracja.find(item => item.strefa === (strefy.find(f=>f.dystansMax === 20000).id)); } if (dystans > 20000) { stawkaKlient = strefaRestauracja.find(item => item.strefa === (strefy.find(f=>f.dystansMax === 30000).id)); } if (dystans > 30000) { stawkaKlient = strefaRestauracja.find(item => item.strefa === (strefy.find(f=>f.dystansMax === 50000).id)); } if (dystans > 40000) { stawkaKlient = strefaRestauracja.find(item => item.strefa === (strefy.find(f=>f.dystansMax === 0).id)); } if (dystans > 50000) { stawkaKlient = { error: true } } } return {stawkaKlient} }; // import Server from "@syncano/core" // export const async obliczStawkeKlient = ( // adres: String, // restauracjaId: Number, // dystans: Number ) => { // console.log(adres, restauracjaId, dystans) // const restauracja = await Promise (data.restauracja // .where("id", restauracjaId) // .firstOrFail()) // let matches = !adres.toLowerCase().match(/białystok/); // let strefa = 1; // let stawka // if (matches) { // strefa = 2; // stawka = 1600; // if (dystans > 8000) { // strefa = 3; // stawka = 2400; // } // if (dystans > 12000) { // strefa = 4; // stawka = 3200; // } // if (dystans > 16000) { // strefa = 5; // stawka = 3800; // } // if (dystans > 20000) { // strefa = 6; // stawka = 6000; // } // if (dystans > 30000) { // strefa = 7; // stawka = 8500; // } // if (dystans > 40000) { // strefa = 8; // stawka = 10000; // } // if (dystans > 50000) { // throw new Error("Nie dowozimy"); // } // } // return {stawka,strefa} // };