@myorders/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Myorders
166 lines (134 loc) • 4.76 kB
JavaScript
import Model, { attr, hasMany, belongsTo } from '@ember-data/model';
import { format, formatDistanceToNow } from 'date-fns';
import { getOwner } from '@ember/application';
import isEmail from '@myorders/console/utils/is-email';
import Store from './store';
export default class NetworkModel extends Model {
/** @ids */
created_by_uuid;
company_uuid;
logo_uuid;
backdrop_uuid;
/** @relationships */
stores;
notification_channels;
gateways;
logo;
backdrop;
/** @attributes */
name;
description;
website;
email;
phone;
facebook;
instagram;
twitter;
tags;
translations;
alertable;
public_id;
key;
currency;
timezone;
online;
stores_count;
options;
logo_url;
backdrop_url;
slug;
/** @dates */
created_at;
updated_at;
/** @computed */
get updatedAgo() {
return formatDistanceToNow(this.updated_at);
}
get updatedAt() {
return format(this.updated_at, 'PPP');
}
get createdAgo() {
return formatDistanceToNow(this.created_at);
}
get createdAt() {
return format(this.created_at, 'PPP p');
}
/** @methods */
toJSON() {
return this.serialize();
}
loadNotificationChannels() {
const owner = getOwner(this);
const store = owner.lookup(`service:store`);
return new Promise((resolve, reject) => {
return store
.query('notification-channel', { owner_uuid: this.id })
.then((notificationChannels) => {
this.notification_channels = notificationChannels.toArray();
resolve(notificationChannels);
})
.catch(reject);
});
}
loadPaymentGateways() {
const owner = getOwner(this);
const store = owner.lookup(`service:store`);
return new Promise((resolve, reject) => {
return store
.query('gateway', { owner_uuid: this.id })
.then((gateways) => {
this.gateways = gateways.toArray();
resolve(gateways);
})
.catch(reject);
});
}
loadStores() {
const owner = getOwner(this);
const store = owner.lookup(`service:store`);
return new Promise((resolve, reject) => {
return store
.query('store', { network: this.id })
.then((stores) => {
this.stores = stores.toArray();
resolve(stores);
})
.catch(reject);
});
}
addStores(stores = [], remove = []) {
const owner = getOwner(this);
const fetch = owner.lookup(`service:fetch`);
stores = stores.map((store) => {
if (store instanceof Store) {
return store.id;
}
return store;
});
remove = remove.map((store) => {
if (store instanceof Store) {
return store.id;
}
return store;
});
return fetch.post(`networks/${this.id}/add-stores`, { stores, remove }, { namespace: 'storefront/int/v1' });
}
removeStores(stores = []) {
const owner = getOwner(this);
const fetch = owner.lookup(`service:fetch`);
stores = stores.map((store) => {
if (store instanceof Store) {
return store.id;
}
return store;
});
return fetch.post(`networks/${this.id}/remove-stores`, { stores }, { namespace: 'storefront/int/v1' });
}
sendInvites(recipients = []) {
const owner = getOwner(this);
const fetch = owner.lookup(`service:fetch`);
// only send to valid recipients
recipients = recipients.filter((email) => isEmail(email));
return fetch.post(`networks/${this.id}/invite`, { recipients }, { namespace: 'storefront/int/v1' });
}
}