Skip to content

Commit

Permalink
feat: notifications (medusajs#172)
Browse files Browse the repository at this point in the history
The Notifications API allows plugins to register Notification Providers which have `sendNotification` and `resendNotification`.

Each plugin can listen to any events transmittet over the event bus and the result of the notification send will be persisted in the database to allow for clear communications timeline + ability to resend notifications.
  • Loading branch information
srindom authored Feb 15, 2021
1 parent 4229e24 commit 7308946
Show file tree
Hide file tree
Showing 46 changed files with 1,533 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,41 @@ class WebshipperFulfillmentService extends FulfillmentService {
/**
* This plugin doesn't support shipment documents.
*/
async getShipmentDocuments() {
return []
async retrieveDocuments(fulfillmentData, documentType) {
switch (documentType) {
case "label":
const labelRelation = fulfillmentData?.relationships?.labels
if (labelRelation) {
const docs = await this.retrieveRelationship(labelRelation)
.then(({ data }) => data)
.catch((_) => [])

return docs.map((d) => ({
name: d.attributes.document_type,
base_64: d.attributes.base64,
type: "application/pdf",
}))
}
return []

case "invoice":
const docRelation = fulfillmentData?.relationships?.documents
if (docRelation) {
const docs = await this.retrieveRelationship(docRelation)
.then(({ data }) => data)
.catch((_) => [])

return docs.map((d) => ({
name: d.attributes.document_type,
base_64: d.attributes.base64,
type: "application/pdf",
}))
}
return []

default:
return []
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/medusa-interfaces/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export { default as BaseModel } from "./base-model"
export { default as PaymentService } from "./payment-service"
export { default as FulfillmentService } from "./fulfillment-service"
export { default as FileService } from "./file-service"
export { default as NotificationService } from "./notification-service"
export { default as OauthService } from "./oauth-service"
28 changes: 28 additions & 0 deletions packages/medusa-interfaces/src/notification-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BaseService from "./base-service"

/**
* Interface for Notification Providers
* @interface
*/
class BaseNotificationService extends BaseService {
constructor() {
super()
}

getIdentifier() {
return this.constructor.identifier
}

/**
* Used to retrieve documents related to a shipment.
*/
sendNotification(event, data) {
throw new Error("Must be overridden by child")
}

resendNotification(notification, config = {}) {
throw new Error("Must be overridden by child")
}
}

export default BaseNotificationService
Loading

0 comments on commit 7308946

Please sign in to comment.