Created
May 11, 2015 21:13
-
-
Save quangpham/9732e99be839ed304602 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.factory "HauserService", [ | |
'$http', '$q', '$state', 'Notification', | |
($http, $q, $state, Notification) -> | |
handleUnauthorizedRequest = -> | |
Notification.error "Login is required" | |
$state.go HauserAppSettings.unauthorizedFallbackUrlState | |
httpRequest = (_method, _url, _data) -> | |
current_user = Hauser.User.current() | |
return handleUnauthorizedRequest() unless current_user | |
defaulConfig = | |
method: _method | |
url: HauserAppSettings.apiBaseUrl + _url | |
headers: | |
"X-User-Email": current_user.email | |
"X-User-Token": current_user.authentication_token | |
"Content-Type": "application/json" | |
defaulConfig.data = _data unless _data == undefined | |
d = $q.defer() | |
$http(defaulConfig).then ((res) -> | |
if res.status == 200 | |
d.resolve res.data | |
), (err) -> | |
if err.status == 401 && err.data.error == "wrong_token" | |
Hauser.User.logOut() | |
$state.go HauserAppSettings.unauthorizedFallbackUrlState | |
console.log err | |
d.reject err | |
d.promise | |
factory = | |
bookings: | |
get_bookings: -> | |
httpRequest("get", "/bookings.json") | |
new_booking: (params) -> | |
httpRequest("post", "/bookings.json", params) | |
delete_booking: (booking_id) -> | |
httpRequest("delete", "/bookings/" + booking_id + ".json") | |
current_user: | |
info: -> | |
httpRequest("get", "/users/me.json") | |
add_house: (house_id, params) -> | |
httpRequest("post", "/users/me/houses/" + house_id + ".json", params) | |
get_groups: -> | |
httpRequest("get", "/users/me/groups.json") | |
get_unread_messages_count: -> | |
httpRequest("get", "/messages/unread/count.json") | |
update_device_token: (params) -> | |
httpRequest("post", "/users/me/device_token.json", params) | |
feeds: | |
get_feed: (feed_id) -> | |
httpRequest("get", "/feeds/" + feed_id + ".json") | |
delete_feed: (feed_id) -> | |
httpRequest("delete", "/feeds/" + feed_id + ".json") | |
like_feed: (feed_id) -> | |
httpRequest("post", "/feeds/" + feed_id + "/like.json") | |
comment_feed: (feed_id, params) -> | |
httpRequest("post", "/feeds/" + feed_id + "/comment.json", params) | |
get_feed_comments: (feed_id) -> | |
httpRequest("get", "/feeds/" + feed_id + "/comments.json") | |
houses: | |
search_by_address: (keyword) -> | |
httpRequest("get", "/houses/search.json?address=" + keyword) | |
list_feeds: (house_id, _feed_type, page) -> | |
httpRequest("get", "/houses/" + house_id + "/feeds.json" + "?feed_type=" + _feed_type + "&page=" + page) | |
new_feeds: (house_id, data) -> | |
httpRequest("post", "/houses/" + house_id + "/feeds.json", data) | |
get_facilities: (house_id) -> | |
httpRequest("get", "/houses/" + house_id + "/facilities.json") | |
get_facility: (house_id, facility_id) -> | |
httpRequest("get", "/houses/" + house_id + "/facilities/" + facility_id + ".json") | |
get_groups: (house_id) -> | |
httpRequest("get", "/houses/" + house_id + "/groups.json") | |
create_group: (house_id, params) -> | |
httpRequest("post", "/houses/" + house_id + "/groups.json", params) | |
accept_group: (house_id, group_id) -> | |
httpRequest("post", "/houses/" + house_id + "/groups/" + group_id + "/accept.json") | |
leave_group: (house_id, group_id) -> | |
httpRequest("post", "/houses/" + house_id + "/groups/" + group_id + "/leave.json") | |
join_group: (house_id, group_id) -> | |
httpRequest("post", "/houses/" + house_id + "/groups/" + group_id + "/join.json") | |
get_info: (house_id) -> | |
httpRequest("get", "/houses/" + house_id + ".json") | |
get_users: (house_id) -> | |
httpRequest("get", "/houses/" + house_id + "/members.json?is_member=true") | |
verify_address: (house_id, verification_code) -> | |
httpRequest("post", "/houses/" + house_id + "/verify.json", {code: verification_code}) | |
messages: | |
get_messages: -> | |
httpRequest("get", "/messages.json") | |
new_messages: (params) -> | |
httpRequest("post", "/messages.json", params) | |
reply_message: (message_id, params) -> | |
httpRequest("post", "/messages/" + message_id + "/reply.json", params) | |
get_threads: (message_id) -> | |
httpRequest("get", "/messages/" + message_id + "/threads.json") | |
notifications: | |
get_notifications: (page)-> | |
httpRequest("get", "/notifications.json?page=" + page) | |
count_unload: -> | |
httpRequest("get", "/notifications/unload/count.json") | |
mark_as_read: (notification_id) -> | |
httpRequest("post", "/notifications/" + notification_id + "/read.json") | |
factory | |
] | |
app.factory "UserService", [ | |
'$http', '$state', '$cordovaFacebook', '$q', '$rootScope', '$ionicLoading', | |
($http, $state, $cordovaFacebook, $q, $rootScope, $ionicLoading) -> | |
factory = {} | |
process_user_info_after_login = (res_data) -> | |
Hauser.User.setData res_data.data | |
$rootScope.$broadcast 'HAUSER_USER_LOGIN_SUCCESSFULLY' | |
unless res_data.data.houses.current_house | |
if res_data.data.houses.unconfirmed.length > 0 | |
return $state.go "menu.notifications" # go to unconfirmed houses page | |
else | |
return $state.go "user.confirm_address" | |
else | |
return $state.go "menu.feed" if res_data.data.houses.current_house | |
#$state.go "confirm.phone" unless res.data.verification.has_phone | |
signInOrSignUpHauserWithFacebookToken = (facebookAccessToken) -> | |
$ionicLoading.show() | |
$http.post(HauserAppSettings.apiBaseUrl + "/sessions/auth_with_facebook_token.json", {token: facebookAccessToken} ).then ((res) -> | |
$ionicLoading.hide() | |
process_user_info_after_login(res) | |
), (err) -> | |
$ionicLoading.hide() | |
console.log "To do: Login not okie" | |
console.log err | |
factory.signInOrSignUpWithFacebook = -> | |
cordovaFbLogin = -> | |
console.log "cordovaFbLogin" | |
$cordovaFacebook.login(["email"]).then ((response) -> | |
if !response.authResponse | |
console.log "error handleing for this" | |
return | |
else | |
signInOrSignUpHauserWithFacebookToken response.authResponse.accessToken | |
), (error) -> | |
console.log error | |
jsFbLogin = -> | |
facebookConnectPlugin.browserInit HauserAppSettings.facebookAppId | |
facebookConnectPlugin.login [ 'email' ], ((response) -> | |
if !response.authResponse | |
console.log "error handleing for this" | |
return | |
else | |
signInOrSignUpHauserWithFacebookToken response.authResponse.accessToken | |
), (error) -> | |
console.log error | |
if !window.cordova | |
jsFbLogin() | |
else | |
cordovaFbLogin() | |
factory.sign_up = (user_info) -> | |
d = $q.defer() | |
$ionicLoading.show() | |
$http.post(HauserAppSettings.apiBaseUrl + "/sessions/sign_up.json", user_info).then ((res) -> | |
d.resolve res | |
$ionicLoading.hide() | |
process_user_info_after_login(res) if res.status == 200 | |
), (err) -> | |
$ionicLoading.hide() | |
d.reject err | |
d.promise | |
factory.sign_in = (user_info) -> | |
d = $q.defer() | |
$ionicLoading.show() | |
$http.post(HauserAppSettings.apiBaseUrl + "/sessions/sign_in.json", user_info).then ((res) -> | |
d.resolve res | |
$ionicLoading.hide() | |
process_user_info_after_login(res) if res.status == 200 | |
), (err) -> | |
$ionicLoading.hide() | |
d.reject err | |
d.promise | |
factory | |
] | |
app.factory "HauserHelper", [ | |
'$http', '$q', '$state', 'Notification', | |
($http, $q, $state, Notification) -> | |
factory = {} | |
factory.bookings = {} | |
factory.bookings.default_booking_rules = -> | |
_default_slots = | |
"7-8": true | |
"8-9": true | |
"9-10": true | |
"10-11": true | |
"11-12": true | |
"12-13": true | |
"13-14": true | |
"14-15": true | |
"15-16": true | |
"16-17": true | |
"17-18": true | |
"18-19": true | |
"19-20": true | |
"20-21": true | |
"21-22": true | |
"22-23": true | |
booking_rules = | |
Su: _default_slots | |
Mo: _default_slots | |
Tu: _default_slots | |
We: _default_slots | |
Th: _default_slots | |
Fr: _default_slots | |
Sa: _default_slots | |
return booking_rules | |
factory | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment