stoptime-rs/src/models/customer.rs

102 lines
3.1 KiB
Rust

use chrono::NaiveDateTime;
use diesel::prelude::*;
use juniper_codegen::GraphQLInputObject;
use crate::models::{Invoice, Task};
use crate::schema::customers;
use crate::DbConn;
/// The customer model.
///
/// This model represents a customer that has projects/tasks for which invoices need to be
/// generated.
#[derive(AsChangeset, Associations, Debug, Identifiable, Queryable)]
#[table_name = "customers"]
pub struct Customer {
/// The unique identification number
pub id: i32,
/// The city part of the address
pub address_city: String,
/// The postal code part of the address
pub address_postal_code: String,
/// The street part of the address
pub address_street: String,
/// The email address
pub email: String,
/// The name of the financial contact person/department
pub financial_contact: String,
/// The default hourly rate (if applicable)
pub hourly_rate: Option<f64>,
/// The official (long) name
pub name: String,
/// The phone number
pub phone: String,
/// The abbreviated name
pub short_name: String,
/// Flag whether the customer requires time specificaions
pub time_specification: bool,
/// The time of creation
pub created_at: NaiveDateTime,
/// The time of last update
pub updated_at: NaiveDateTime,
}
impl Customer {
/// Returns the invoices billed to the customer.
pub fn invoices(&self, conn: &DbConn) -> QueryResult<Vec<Invoice>> {
Invoice::belonging_to(self).load(&**conn)
}
/// Returns the project/tasks associated with the customer.
pub fn tasks(&self, conn: &DbConn) -> QueryResult<Vec<Task>> {
Task::belonging_to(self).load(&**conn)
}
}
/// The new customer model
///
/// This model represents a new customer that can be inserted into the database.
#[derive(Default, GraphQLInputObject, Insertable)]
#[table_name = "customers"]
pub struct NewCustomer {
/// The city part of the address
pub address_city: String,
/// The postal code part of the address
pub address_postal_code: String,
/// The street part of the address
pub address_street: String,
/// The email address
pub email: String,
/// The name of the financial contact person/department
pub financial_contact: String,
/// The default hourly rate (if applicable)
pub hourly_rate: Option<f64>,
/// The official (long) name
pub name: String,
/// The phone number
pub phone: String,
/// The abbreviated name
pub short_name: String,
/// Flag whether the customer requires time specificaions
pub time_specification: bool,
}
mod graphql {
use super::{Customer, DbConn, Invoice, Task};
use juniper::{object, FieldResult};
#[object(Context = DbConn)]
impl Customer {
/// Returns the invoices billed to the customer.
fn invoices(&self, context: &DbConn) -> FieldResult<Vec<Invoice>> {
self.invoices(context).map_err(Into::into)
}
/// Returns the project/tasks associated with the customer.
fn tasks(&self, context: &DbConn) -> FieldResult<Vec<Task>> {
self.tasks(context).map_err(Into::into)
}
}
}