Skip to content

Instantly share code, notes, and snippets.

@ubaltaci
Last active June 25, 2022 02:06
Show Gist options
  • Save ubaltaci/f4245c32e650ccba60f7 to your computer and use it in GitHub Desktop.
Save ubaltaci/f4245c32e650ccba60f7 to your computer and use it in GitHub Desktop.
mongo
// Tracker document can only be created/edited/deleted by admins.
var Tracker = {
tracker_imei: {
type: String,
required: true,
unique: true
},
/**
* Country phone codes, customer can select from list,
* `Turkey` is pre-selected. ( +90 )
*/
tracker_phone_country_code: String,
/**
* Phone number in format (XXX) XXX XX XX
* shoud be pre-filled with (5XX) XXX XX XX
* Use some jquery input mask in front end.
*/
tracker_phone_number: {
type: String,
required: true,
unique: true
},
/**
* Related to the payment
* Defaults to false
*/
tracker_status: Boolean,
/**
* Tracker has only one user.
* When `tracker_owner_customer` assigned, `tracker_sales_at` must be updated with `Date.Now`
*/
tracker_owner_customer: {
type: Mongoose.ObjectId,
ref: "Customer"
},
tracker_sold_at: Date,
/**
* name aka Alias, can be set by tracker_owner_customer
*/
tracker_name: String,
/**
* S3 URL,
* Predefined avatars also available on S3 with url`s.
* Customer can select pre-defined avatars from list or
* upload new image with input field on web or upload image
* from mobile ( Both gallery and camera options should be available )
*/
tracker_avatar: String,
/**
* Should be in format: DD
*/
tracker_plate: String,
/**
* Embedded Document Array: tracker_shared_with_customers
*
* You should check against customer can be inserted only one time with spesific permission,
* same customer can not be added twice.
*/
tracker_shared_with_customers: [{
type: Mongoose.ObjectId,
ref: "Customer"
}],
/**
* Reference Document Array: tracker_history
*/
tracker_history: [{
type: Mongoose.ObjectId,
ref: "TrackerHistory"
}],
/**
* Embedded document: tracker_settings
* Only "tracker_owner Customer" and
* CMS admins can change settings.
*/
tracker_mode: String, // parking, lost, riding from enum.
/**
* With succesfull payment create new tracker_subscription and push into
* this document array.
* start_at immediately set to Date.now, when tracker subscription
* cancelled by user, get last tracker_subscription element and set
* end_at to Date.now.
*/
tracker_subscription: [{
start_at: Date,
end_at: Date
}]
};
var TrackerHistory = {
tracker_id: {
type: Mongoose.ObjectId,
ref: "Tracker"
},
// Copied from current tracker_mode
tracker_mode: String,
// Enum,
// Location, Stealing, InternalBatterIsLow, ExternalBatteryIsCut, ExternalBatteryIsOn, GPSAntennaCut
// Location set when Location info comes to CMS
// Others are related to Alarms and set with related alarm name.
tracker_history_type: String,
tracker_history_latitude: String,
tracker_history_longitude: String,
tracker_history_total_mileage: Number,
tracker_history_total_runtime: Number,
tracker_history_gsm_strength: Boolean,
tracker_history_gps_valid: Boolean,
tracker_history_gps_satellite_number: Number,
tracker_history_speed: Number,
tracker_history_direction: Number,
tracker_history_altitude: Number,
tracker_history_positioning_accuracy: Number,
// Out of 100
tracker_history_battery_level: Number,
tracker_history_created_at: Date,
// RAW data coming from tracker should also be store as a mixed type.
tracker_history_raw: Mongoose.Types.Mixed
};
// When customer who owns a tracker initiates a share request,
// This document created.
// customer_share and customer_shared can not be same customer.
// customer_share must be owner of that tracker.
// Share tracker requests are expired in 72 hour.
// TrackerShare document can be removed / denied by both customers.
// When shared customer accepted it, add to related tracker documents `tracker_shared_with`
// TODO: Uğur, not registered users
var TrackerShareRequest = {
customer_share: {
type: Mongoose.ObjectId,
ref: "Customer"
},
customer_shared: {
type: Mongoose.ObjectId,
ref: "Customer"
},
// Use mongo ttl
createdAt: {
"type": Date,
"default": Date.now(),
"expires": 3 * 24 * 60 * 60 // 72 Hour
}
};
// Customer can sign-up directly from web or mobile,
// Send e-mail with activation url contains (mail_validation_token and e-mail)
// when customer click that url check if user_password previously set,
// open login page with "Thank you, successfully verified your e-mail"
// Login page;
// If customer_mail_is_validated is false,
// do not logged in him, give warning, you have to validate your e-mail first.
// with a button to send activation e-mail again.
var Customer = {
customer_email: {
type: String,
required: true,
unique: true
},
customer_mail_validation_token: {
type: Mongoose.ObjectId,
ref: "Token"
},
// defaults to false
customer_email_is_validated: Boolean,
/**
* Country phone codes, customer can select from list,
* `Turkey` is pre-selected. ( +90 )
* Use some jquery input mask in front end.
*/
customer_phone_country_code: String,
/**
* Phone number in format (XXX) XXX XX XX
* shoud be pre-filled with (5XX) XXX XX XX
*/
customer_phone: {
type: String,
required: true,
unique: true
},
customer_name: {
type: String,
required: true
},
customer_surname: {
type: String,
required: true
},
/**
* Embedded Document: customer_address
* Except for customer_address_postal_code all fields required
*/
customer_address: {
// Country list, Turkey with pre-selected.
customer_address_country: String, // select
customer_address_province: String, // textfield
customer_address_district: String, // textfield
customer_address_detail: String, // textarea, max to 200char.
customer_postal_code: String // textfield
},
// Password must be kept in hashed format.
customer_password: String,
customer_password_reset_token: {
type: Mongoose.ObjectId,
ref: "Token"
},
/**
* Embedded Document Array: customer_owned_trackers
* CMS admin can assign trackers from list which `tracker_owner_customer` field is null.
*/
customer_owned_trackers: [{
type: Mongoose.ObjectId,
ref: "Tracker"
}],
/**
* Embedded Document Array: customer_followed_trackers
* When TrackerShareRequest come and user accept it,
* you should push related tracker id to this array.
*/
customer_followed_trackers: [{
type: Mongoose.ObjectId,
ref: "Tracker"
}],
customer_last_seen_at_app: Date,
customer_last_seen_at_web: Date,
customer_created_at: Date,
};
// Multiple use case token,
// 1. E-Mail Validation
// 2. Reset Password
// 3. ...
var Token = {
token_value: String,
token_expired_at: {
"type": Date,
"default": Date.now(),
"expires": 3 * 24 * 60 * 60 // 72 Hour
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment