Skip to content

Commit

Permalink
Merge pull request #446 from crossroads/master
Browse files Browse the repository at this point in the history
September Live Release #1
  • Loading branch information
abulsayyad123 authored Sep 4, 2020
2 parents 066609a + 8af0d49 commit 49d5097
Show file tree
Hide file tree
Showing 19 changed files with 360 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ jobs:
android_build_and_deploy:
<<: *defaults
docker:
- image: circleci/android:api-26-node8-alpha
- image: circleci/android:api-29-node@sha256:f57cbd442b1d90d01a672e87fa9c516a71312376641d70aee67736e78cb9645f
environment:
JVM_OPTS: -Xmx3200m
KEYSTORE: goodcity.keystore
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-notes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
with:
node-version: 12.x
- name: Generate and email notes
run: npx @goodcity/release-notes --email-to "[email protected],[email protected],[email protected]" --head ${{github.event.pull_request.head.sha}} --base ${{github.event.pull_request.base.sha}} --email-subject "🚀 Admin Release 🚀"
run: npx @goodcity/release-notes --unshallow --email-to "[email protected],[email protected],[email protected]" --head ${{github.event.pull_request.head.sha}} --base ${{github.event.pull_request.base.sha}} --email-subject "🚀 Admin Release 🚀"
env:
JIRA_USERNAME: ${{secrets.jira_username}}
JIRA_PASSWORD: ${{secrets.jira_password}}
Expand Down
185 changes: 105 additions & 80 deletions app/controllers/my_notifications.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { AjaxBuilder } from "goodcity/utils/ajax-promise";
import Ember from "ember";
import offers from "./offers";
import _ from "lodash";
import AjaxPromise from "goodcity/utils/ajax-promise";

const { computed } = Ember;

const MSG_KEY = msg => {
return [
msg.get("isPrivate") ? "private" : "public",
msg.get("offerId") || "-",
msg.get("itemId") || "-"
msg.get("messageableType") || "-",
msg.get("messageableId") || "-"
].join("/");
};

export default offers.extend({
sortProperties: ["createdAt:desc"],
sortedModel: Ember.computed.sort("model", "sortProperties"),
export default Ember.Controller.extend({
messagesUtil: Ember.inject.service("messages"),
subscriptions: Ember.inject.controller(),
store: Ember.inject.service(),
Expand All @@ -26,31 +23,47 @@ export default offers.extend({
showUnread: true,
notifications: [],

init() {
on() {
// When a new message arrives, we move it to the top
this.get("subscriptions").on("create:message", ({ id }) => {
const store = this.get("store");
const msg = store.peekRecord("message", id);
const offerId = msg && msg.get("offerId");
const notifications = this.get("notifications");

if (!offerId) {
return;
}
this.get("subscriptions").on(
"create:message",
this,
this.onNewNotification
);
},

this.loadIfAbsent("offer", offerId).then(() => {
let notif = notifications.findBy("key", MSG_KEY(msg));
if (notif) {
// Update existing one
notifications.removeObject(notif);
notif.get("messages").addObject(msg);
} else {
// Create new one
notif = this.messagesToNotification([msg]);
}
notifications.insertAt(0, notif);
});
});
off() {
this.get("subscriptions").off(
"create:message",
this,
this.onNewNotification
);
},

onNewNotification({ id }) {
const store = this.get("store");
const msg = store.peekRecord("message", id);
const messageableId = msg.get("messageableId");
const notifications = this.get("notifications");

if (!messageableId) {
return;
}

let notif = notifications.findBy("key", MSG_KEY(msg));

if (notif) {
// Update existing one
notifications.removeObject(notif);
msg.set("unreadCount", +notif.get("unreadCount") + 1);
notif.get("messages").addObject(msg);
} else {
// Create new one
msg.set("unreadCount", 1);
notif = this.messagesToNotification([msg]);
}

notifications.insertAt(0, notif);
},

/**
Expand All @@ -60,46 +73,67 @@ export default offers.extend({
* @returns
*/
messagesToNotification(messages) {
const props = ["id", "itemId", "offer", "sender", "createdAt", "isPrivate"];
const lastMessage = messages.sortBy("createdAt").get("lastObject");
const item =
lastMessage.get("itemId") &&
this.get("store").peekRecord("item", lastMessage.get("itemId"));
const props = [
"id",
"itemId",
"offerId",
"sender",
"createdAt",
"isPrivate"
];

let item, offer;
const lastMessage = messages.sortBy("id").get("lastObject");
let messageableType = lastMessage.get("messageableType");
let recordId = lastMessage.get("messageableId");

if (messageableType === "Item") {
item =
this.get("store").peekRecord("item", recordId) ||
this.get("store").findRecord("item", recordId);
} else if (messageableType === "Offer") {
offer =
this.get("store").peekRecord("offer", recordId) ||
this.get("store").findRecord("offer", recordId);
}

let notification = Ember.Object.create(lastMessage.getProperties(props));
notification.setProperties({
key: MSG_KEY(lastMessage),
item: item,
messages: messages,
isSingleMessage: computed.equal("messages.length", 1),
isSingleMessage: computed.equal("unreadCount", 1),
isThread: computed.not("isSingleMessage"),
offerId: computed.alias("messages.firstObject.offerId"),
offer: offer,
text: computed("messages.[]", function() {
return this.get("messages")
.sortBy("createdAt")
.sortBy("id")
.get("lastObject.body");
}),
unreadCount: computed("[email protected]", function() {
return this.get("messages")
.filterBy("isUnread")
.get("length");
unreadCount: computed("[email protected]", "messages.[]", {
get() {
let lastMessage = this.get("messages")
.sortBy("id")
.get("lastObject");
return lastMessage.get("unreadCount");
},
set(key, value) {
return value;
}
})
});
return notification;
},

/**
* Transform offers into "notifications" object with more UI-friendly properties
*
* @param {Offer} offer
* @returns {Object}
*/
buildNotifications(offer) {
const offerMessages = offer.get("messages").filter(msg => {
buildNotifications(messages) {
const groupedMessages = messages.filter(msg => {
return this.get("showUnread") ? msg.get("isUnread") : true;
});

return _.chain(offerMessages)
return _.chain(groupedMessages)
.groupBy(MSG_KEY)
.map(msgs => this.messagesToNotification(msgs))
.value();
Expand All @@ -109,51 +143,42 @@ export default offers.extend({
* Injects API JSON into the store and returns a list of models
*
* @param {Object} data
* @returns {Offer[]}
* @returns {Message[]}
*/
toOfferModels(data) {
toMessageModels(data) {
this.get("store").pushPayload(data);
return data["offers"].map(({ id }) => {
return this.get("store").peekRecord("offer", id);
return data.messages.map(({ id }) => {
return this.get("store").peekRecord("message", id);
});
},

/**
* Loads a record from either the store or the api
*
* @param {String} model
* @param {String} id
* @returns {Model}
*/
loadIfAbsent(model, id) {
const store = this.get("store");
return Ember.RSVP.resolve(
store.peekRecord(model, id) || store.findRecord(model, id)
);
},

actions: {
/**
* Loads a page of offers
* Loads a page of Message Notifications
* Used by the infinite list
*
* @param {*} pageNo
* @returns
*/
loadMoreMessages(pageNo) {
const state = this.get("showUnread") ? "unread" : "all";

return new AjaxBuilder("/offers/search")
.withAuth(this.get("session.authToken"))
.withQuery({
with_notifications: state,
include_messages: true,
recent_offers: true
})
.getPage(pageNo, 10)
.then(data => this.toOfferModels(data))
.then(offers => {
const notifications = _.chain(offers)
const state = this.get("showUnread") ? "unread" : "";

const params = {
page: pageNo,
state: state,
messageable_type: ["offer", "item"]
};

return new AjaxPromise(
"/messages/notifications",
"GET",
this.get("session.authToken"),
params
)
.then(data => this.toMessageModels(data))
.then(messages => {
const notifications = _.chain(messages)
.groupBy(MSG_KEY)
.map(o => this.buildNotifications(o))
.flatten()
.value();
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/receive_package.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default Ember.Controller.extend(AsyncTasksMixin, {
}),

allAvailablePrinter: Ember.computed(function() {
return this.get("printerService").allAvailablePrinter();
return this.get("printerService").allAvailablePrinters();
}),

selectedPrinterDisplay: Ember.computed("selectedPrinterId", function() {
Expand Down
3 changes: 2 additions & 1 deletion app/models/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export default Model.extend({
name: attr("string"),
user: hasMany("user", {
async: false
})
}),
printersUsers: hasMany("printersUsers", { async: false })
});
12 changes: 12 additions & 0 deletions app/models/printers_user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ember from "ember";
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo } from "ember-data/relationships";

export default Model.extend({
printerId: attr("number"),
userId: attr("number"),
tag: attr("string"),
printer: belongsTo("printer", { async: false }),
user: belongsTo("user", { async: false })
});
6 changes: 6 additions & 0 deletions app/routes/my_notifications.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import AuthorizeRoute from "./authorize";

export default AuthorizeRoute.extend({
setupController(controller, model) {
this._super(controller, model);
controller.on();
},

resetController: function(controller, isExiting) {
this._super.apply(this, arguments);
controller.off();

if (isExiting) {
controller.set("notifications", []);
Expand Down
18 changes: 11 additions & 7 deletions app/routes/receive_package.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ export default AuthorizeRoute.extend({
},

setupPrinterId(controller) {
let allAvailablePrinters = this.get("printerService").allAvailablePrinter();
let user = this.get("session.loggedInUser");
if (user.get("printerId")) {
controller.set("selectedPrinterId", user.get("printerId"));
let defaultPrinter = this.get("printerService").getDefaultPrinter();
if (defaultPrinter) {
controller.set("selectedPrinterId", defaultPrinter.id);
} else {
let firstPrinterId = allAvailablePrinters[0].id;
this.get("printerService").updateUserDefaultPrinter(firstPrinterId);
controller.set("selectedPrinterId", firstPrinterId);
let allAvailablePrinters = this.get(
"printerService"
).allAvailablePrinters();
if (allAvailablePrinters.length) {
let firstPrinterId = allAvailablePrinters[0].id;
this.get("printerService").addDefaultPrinter(firstPrinterId);
controller.set("selectedPrinterId", firstPrinterId);
}
}
},

Expand Down
Loading

0 comments on commit 49d5097

Please sign in to comment.