Skip to content

Commit

Permalink
feat: ✨ im the best
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrnoch committed Jul 24, 2024
1 parent 207f188 commit 860da95
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 23 deletions.
49 changes: 49 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ log = "0.4"
thiserror = "1.0.61"
platform-dirs = "0.3.0"
anyhow = "1.0.86"
bcrypt = "0.15.1"
# specta = "=2.0.0-rc.12"
# tauri-specta = { version = "=2.0.0-rc.11", features = ["javascript", "typescript"] }

Expand Down
1 change: 1 addition & 0 deletions src-tauri/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ model Company {
currencies Currency[]
clients Client[]
settings Settings?
password String?
cin String @unique // IČO
vatId String? @unique // DIČ
Expand Down
64 changes: 57 additions & 7 deletions src-tauri/src/commands/company.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@ use crate::company;
use crate::currency;
use crate::template;
use crate::DbState;
use bcrypt::{hash, DEFAULT_COST};
use prisma_client_rust::not;
use prisma_client_rust::QueryError;
use serde::Deserialize;

#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompanyWithProtection {
id: i32,
name: String,
email: Option<String>,
is_protected: bool,
}

#[tauri::command]
pub async fn get_companies(
client: DbState<'_>,
exclude: Option<i32>,
) -> Result<Vec<company::Data>, QueryError> {
println!("Getting companies, exluding {:?}", exclude);
let data = client
) -> Result<Vec<CompanyWithProtection>, QueryError> {
println!("Getting companies, excluding {:?}", exclude);
let companies = client
.company()
.find_many(vec![not![company::id::equals(exclude.unwrap_or(999))]])
.exec()
.await;
.await?;

println!("{:?}", data);
data
Ok(companies
.into_iter()
.map(|c| CompanyWithProtection {
id: c.id,
name: c.name,
email: c.email,
is_protected: c.password.is_some(),
})
.collect())
}

#[tauri::command]
Expand All @@ -45,9 +62,9 @@ pub struct ManageCompanyData {
zip: String,
phone: Option<String>,
email: Option<String>,

bank_account: Option<String>,
bank_iban: Option<String>,
password: Option<String>,
}

#[tauri::command]
Expand All @@ -56,6 +73,14 @@ pub async fn create_company(
data: ManageCompanyData,
) -> Result<i32, QueryError> {
debug!("Creating company {:?}", data);

let password_hash = match data.password {
Some(password) => Some(
hash(password, DEFAULT_COST).map_err(|e| QueryError::PasswordHashing(e.to_string()))?,
),
None => None,
};

let company = client
.company()
.create(
Expand All @@ -70,6 +95,7 @@ pub async fn create_company(
company::phone::set(data.phone),
company::bank_account::set(data.bank_account),
company::bank_iban::set(data.bank_iban),
company::password::set(password_hash),
],
)
.exec()
Expand Down Expand Up @@ -168,3 +194,27 @@ pub async fn delete_company(client: DbState<'_>, id: i32) -> Result<(), String>
Err(e) => Err(e.to_string()),
}
}

#[tauri::command]
pub async fn validate_company_password(
client: DbState<'_>,
id: i32,
password: String,
) -> Result<bool, QueryError> {
let company = client
.company()
.find_first(vec![company::id::equals(id)])
.exec()
.await?;

match company {
Some(c) => {
if let Some(hash) = c.password {
Ok(bcrypt::verify(password, &hash).unwrap_or(false))
} else {
Ok(false)
}
}
None => Ok(false),
}
}
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async fn main() {
commands::db::check_db,
commands::db::migrate_and_populate,
commands::company::get_company,
commands::company::validate_company_password,
commands::company::create_company,
commands::company::get_companies,
commands::company::delete_company,
Expand Down
2 changes: 1 addition & 1 deletion src/Loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Loader: Component = () => {

const startup = async () => {
const data = await checkDb();
navigate(data === 200 ? "/dashboard" : "/setup");
navigate(data === 200 ? "/login" : "/setup");
};

startup();
Expand Down
5 changes: 5 additions & 0 deletions src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export async function migrateAndPopulate() {
export async function getCompanies(exclude?: number) {
return await invoke<Company[]>("get_companies", { exclude });
}
export async function validateCompanyPassword(id: number, password: string) {
return await invoke<boolean>("validate_company_password", { id, password });
}

export async function getTemplates(indicies: Indicies, templateType?: "INVOICE" | "PROFORMA" | "RECEIVE") {
return await invoke<Template[]>("get_templates", { companyId: state.companyId, indicies, templateType });
Expand Down Expand Up @@ -301,6 +304,8 @@ export type Company = {

bankAccount: string | null;
bankIban: string | null;

isProtected: boolean;
};

export type Template = {
Expand Down
4 changes: 3 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ClientDetail = lazy(() => import("./screens/Dashboard/pages/Other/Clients/
const ManageClient = lazy(() => import("./screens/Dashboard/pages/Other/Clients/ManageClient"));
const Settings = lazy(() => import("./screens/Dashboard/pages/Settings"));
const Dashboard = lazy(() => import("./screens/Dashboard"));
const Login = lazy(() => import("./screens/Login"));
const Setup = lazy(() => import("./screens/Setup"));
const App = lazy(() => import("./App"));
const Loader = lazy(() => import("./Loader"));
Expand All @@ -40,6 +41,7 @@ render(
<Route path="/" component={Loader} />
<Route path="/print/:id" component={Print} />
<Route path="/setup" component={Setup} />
<Route path="/login" component={Login} />
<Route path="/dashboard" component={Dashboard}>
<Route path="/" component={Overview} />
<Route path="/sales">
Expand Down Expand Up @@ -78,7 +80,7 @@ render(
</Route>
</Route>
<Route path="/settings" component={Settings} />
<Route path="*" component={() => <Navigate href={"/dashboard"} />} />
<Route path="*" component={() => <Navigate href={"/login"} />} />
</Route>
</Router>
),
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const Dashboard: ParentComponent = (props) => {

const fetchCompany = async (companyId: number) => {
if (companyId === 0) {
navigate("/setup");
navigate("/login");
return;
}

const companyData = await getCompany(companyId);
if (!companyData) {
navigate("/setup");
navigate("/login");
toast.error("Company not found");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ const ManageCurrency: Component = () => {
</form.Field>
<form.Field name="rate" validators={{ onChange: z.number().min(0), onChangeAsyncDebounceMs: 500 }}>
{(field) => (
<Input
float
<float
type="number"
label={t("pages.other.currencies.form.rate")}
defaultValue={field().state.value}
Expand Down
Loading

0 comments on commit 860da95

Please sign in to comment.