Feat/note on order (#399)

* added NoteService and related endpoints && tests

* removed snapshots

* corrected error in service

* removed snapshot

* added the ability to note down author using a string

* updated model for note

* refactored to access logged in user

* added other user id option

* removed snapshot

* updated according to feedback

* removed snapshots

* reintroduced snapshots

* updated to snake case

* removed try catch from use-db
This commit is contained in:
Sebastian Mateos Nicolajsen
2021-09-22 15:19:35 +02:00
committed by GitHub
parent a82332da3e
commit 897ccf475a
18 changed files with 1054 additions and 46 deletions
@@ -22,6 +22,7 @@ import variantRoutes from "./variants"
import draftOrderRoutes from "./draft-orders"
import collectionRoutes from "./collections"
import notificationRoutes from "./notifications"
import noteRoutes from "./notes"
const route = Router()
@@ -68,6 +69,7 @@ export default (app, container, config) => {
collectionRoutes(route)
notificationRoutes(route)
returnReasonRoutes(route)
noteRoutes(route)
return app
}
@@ -0,0 +1,63 @@
import { MedusaError, Validator } from "medusa-core-utils"
/**
* @oas [post] /notes
* operationId: "PostNotes"
* summary: "Creates a Note"
* description: "Creates a Note which can be associated with any resource as required."
* requestBody:
* content:
* application/json:
* schema:
* properties:
* resource_id:
* type: string
* description: The id of the resource which the Note relates to.
* resource_type:
* type: string
* description: The type of resource which the Note relates to.
* value:
* type: string
* description: The content of the Note to create.
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* note:
* $ref: "#/components/schemas/note"
*
*/
export default async (req, res) => {
const schema = Validator.object().keys({
resource_id: Validator.string(),
resource_type: Validator.string(),
value: Validator.string(),
})
const userId = req.user.id || req.user.userId
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const noteService = req.scope.resolve("noteService")
const result = await noteService.create({
resource_id: value.resource_id,
resource_type: value.resource_type,
value: value.value,
author_id: userId,
})
res.status(200).json({ note: result })
} catch (err) {
throw err
}
}
@@ -0,0 +1,35 @@
/**
* @oas [delete] /notes/{id}
* operationId: "DeleteNotesNote"
* summary: "Deletes a Note"
* description: "Deletes a Note."
* parameters:
* - (path) id=* {string} The id of the Note to delete.
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* id:
* type: string
* description: The id of the deleted Note.
* deleted:
* type: boolean
* description: Whether or not the Note was deleted.
*/
export default async (req, res) => {
const { id } = req.params
try {
const noteService = req.scope.resolve("noteService")
await noteService.delete(id)
res.status(200).json({ id, deleted: true })
} catch (err) {
throw err
}
}
@@ -0,0 +1,31 @@
/**
* @oas [get] /notes/{id}
* operationId: "GetNoteNote"
* summary: "Get Note"
* description: "Retrieves a single note using its id"
* parameters:
* - (path) id=* {string} The id of the note to retrieve.
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* note:
* $ref: "#/components/schemas/note"
*/
export default async (req, res) => {
const { id } = req.params
try {
const noteService = req.scope.resolve("noteService")
const note = await noteService.retrieve(id, { relations: ["author"] })
res.status(200).json({ note })
} catch (err) {
throw err
}
}
@@ -0,0 +1,20 @@
import { Router } from "express"
import middlewares from "../../../middlewares"
const route = Router()
export default app => {
app.use("/notes", route)
route.get("/:id", middlewares.wrap(require("./get-note").default))
route.get("/", middlewares.wrap(require("./list-notes").default))
route.post("/", middlewares.wrap(require("./create-note").default))
route.post("/:id", middlewares.wrap(require("./update-note").default))
route.delete("/:id", middlewares.wrap(require("./delete-note").default))
return app
}
@@ -0,0 +1,42 @@
/**
* @oas [get] /notes
* operationId: "GetNotes"
* summary: "List Notes"
* description: "Retrieves a list of notes"
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* notes:
* type: array
* items:
* $ref: "#/components/schemas/note"
*/
export default async (req, res) => {
try {
const limit = parseInt(req.query.limit) || 50
const offset = parseInt(req.query.offset) || 0
const selector = {}
if ("resource_id" in req.query) {
selector.resource_id = req.query.resource_id
}
const noteService = req.scope.resolve("noteService")
const notes = await noteService.list(selector, {
take: limit,
skip: offset,
relations: ["author"],
})
res.status(200).json({ notes })
} catch (err) {
throw err
}
}
@@ -0,0 +1,51 @@
import { MedusaError, Validator } from "medusa-core-utils"
/**
* @oas [post] /notes/{id}
* operationId: "PostNotesNote"
* summary: "Updates a Note"
* description: "Updates a Note associated with some resource"
* parameters:
* - (path) id=* {string} The id of the Note to update
* requestBody:
* content:
* application/json:
* schema:
* properties:
* value:
* type: string
* description: The updated description of the Note.
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* note:
* $ref: "#/components/schemas/note"
*
*/
export default async (req, res) => {
const { id } = req.params
const schema = Validator.object().keys({
value: Validator.string(),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const noteService = req.scope.resolve("noteService")
const result = await noteService.update(id, value.value)
res.status(200).json({ note: result })
} catch (err) {
throw err
}
}
+1
View File
@@ -45,3 +45,4 @@ export { Swap } from "./models/swap"
export { User } from "./models/user"
export { DraftOrder } from "./models/draft-order"
export { ReturnReason } from "./models/return-reason"
export { Note } from "./models/note"
@@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from "typeorm"
export class addNotes1632220294687 implements MigrationInterface {
name = "addNotes1632220294687"
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "note" ("id" character varying NOT NULL, "value" character varying NOT NULL, "resource_type" character varying NOT NULL, "resource_id" character varying NOT NULL, "author_id" character varying, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "metadata" jsonb, CONSTRAINT "PK_96d0c172a4fba276b1bbed43058" PRIMARY KEY ("id"))`
)
await queryRunner.query(
`CREATE INDEX "IDX_f74980b411cf94af523a72af7d" ON "note" ("resource_type") `
)
await queryRunner.query(
`CREATE INDEX "IDX_3287f98befad26c3a7dab088cf" ON "note" ("resource_id") `
)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_3287f98befad26c3a7dab088cf"`)
await queryRunner.query(`DROP INDEX "IDX_f74980b411cf94af523a72af7d"`)
await queryRunner.query(`DROP TABLE "note"`)
}
}
+96
View File
@@ -0,0 +1,96 @@
import {
Entity,
BeforeInsert,
Column,
DeleteDateColumn,
CreateDateColumn,
UpdateDateColumn,
Index,
JoinColumn,
PrimaryColumn,
ManyToOne,
} from "typeorm"
import { ulid } from "ulid"
import { User } from "./user"
import { resolveDbType, DbAwareColumn } from "../utils/db-aware-column"
@Entity()
export class Note {
@PrimaryColumn()
id: string
@Column()
value: string
@Index()
@Column()
resource_type: string
@Index()
@Column()
resource_id: string
@Column({ nullable: true })
author_id: string
@ManyToOne(() => User)
@JoinColumn({ name: "author_id" })
author: User
@CreateDateColumn({ type: resolveDbType("timestamptz") })
created_at: Date
@UpdateDateColumn({ type: resolveDbType("timestamptz") })
updated_at: Date
@DeleteDateColumn({ type: resolveDbType("timestamptz") })
deleted_at: Date
@DbAwareColumn({ type: "jsonb", nullable: true })
metadata: any
@BeforeInsert()
private beforeInsert() {
if (this.id) return
const id = ulid()
this.id = `note_${id}`
}
}
/**
* @schema note
* title: "Note"
* description: "Notes are elements which we can use in association with different resources to allow users to describe additional information in relation to these."
* x-resourceId: note
* properties:
* id:
* description: "The id of the Note. This value will be prefixed by `note_`."
* type: string
* resource_type:
* description: "The type of resource that the Note refers to."
* type: string
* resource_id:
* description: "The id of the resource that the Note refers to."
* type: string
* value:
* description: "The contents of the note."
* type: string
* author:
* description: "The author of the note."
* type: User
* created_at:
* description: "The date with timezone at which the resource was created."
* type: string
* format: date-time
* updated_at:
* description: "The date with timezone at which the resource was last updated."
* type: string
* format: date-time
* deleted_at:
* description: "The date with timezone at which the resource was deleted."
* type: string
* format: date-time
* metadata:
* description: "An optional key-value map with additional information."
* type: object
*/
+5
View File
@@ -0,0 +1,5 @@
import { EntityRepository, Repository } from "typeorm"
import { Note } from "../models/note"
@EntityRepository(Note)
export class NoteRepository extends Repository<Note> {}
@@ -0,0 +1,202 @@
import NoteService from "../note"
import { MockManager, MockRepository, IdMap } from "medusa-test-utils"
import { EventBusServiceMock } from "../__mocks__/event-bus"
describe("NoteService", () => {
describe("list", () => {
const noteRepo = MockRepository({
find: q => {
return Promise.resolve([
{ id: IdMap.getId("note"), value: "some note" },
])
},
})
const noteService = new NoteService({
manager: MockManager,
noteRepository: noteRepo,
})
beforeAll(async () => {
jest.clearAllMocks()
})
it("calls note model functions", async () => {
await noteService.list(
{ resource_id: IdMap.getId("note") },
{
relations: ["author"],
}
)
expect(noteRepo.find).toHaveBeenCalledTimes(1)
expect(noteRepo.find).toHaveBeenCalledWith({
where: {
resource_id: IdMap.getId("note"),
},
relations: ["author"],
})
})
})
describe("retrieve", () => {
const noteRepo = MockRepository({
findOne: q => {
switch (q.where.id) {
case IdMap.getId("note"):
return Promise.resolve({
id: IdMap.getId("note"),
value: "some note",
})
default:
return Promise.resolve()
}
},
})
const noteService = new NoteService({
manager: MockManager,
noteRepository: noteRepo,
})
beforeAll(async () => {
jest.clearAllMocks()
})
it("calls note model functions", async () => {
await noteService.retrieve(IdMap.getId("note"), { relations: ["author"] })
expect(noteRepo.findOne).toHaveBeenCalledTimes(1)
expect(noteRepo.findOne).toHaveBeenCalledWith({
where: { id: IdMap.getId("note") },
relations: ["author"],
})
})
it("fails when note is not found", async () => {
await expect(
noteService.retrieve(IdMap.getId("not-existing"))
).rejects.toThrow(
`Note with id: ${IdMap.getId("not-existing")} was not found.`
)
})
})
describe("create", () => {
const note = {
id: IdMap.getId("note"),
author_id: IdMap.getId("user"),
}
const noteRepo = MockRepository({
create: f => Promise.resolve(note),
save: f => Promise.resolve(note),
})
const noteService = new NoteService({
manager: MockManager,
noteRepository: noteRepo,
eventBusService: EventBusServiceMock,
})
beforeAll(async () => {
jest.clearAllMocks()
})
it("calls note model functions", async () => {
await noteService.create({
resource_id: IdMap.getId("resource-id"),
resource_type: "type",
value: "my note",
author_id: IdMap.getId("user"),
})
expect(noteRepo.create).toHaveBeenCalledTimes(1)
expect(noteRepo.create).toHaveBeenCalledWith({
resource_id: IdMap.getId("resource-id"),
resource_type: "type",
value: "my note",
author_id: IdMap.getId("user"),
metadata: {},
})
expect(noteRepo.save).toHaveBeenCalledTimes(1)
expect(noteRepo.save).toHaveBeenCalledWith({
id: IdMap.getId("note"),
author_id: IdMap.getId("user"),
})
expect(EventBusServiceMock.emit).toHaveBeenCalledTimes(1)
expect(EventBusServiceMock.emit).toHaveBeenCalledWith(
NoteService.Events.CREATED,
{ id: IdMap.getId("note") }
)
})
})
describe("update", () => {
const note = { id: IdMap.getId("note") }
const noteRepo = MockRepository({
findOne: f => Promise.resolve(note),
save: f => Promise.resolve(note),
})
const noteService = new NoteService({
manager: MockManager,
noteRepository: noteRepo,
eventBusService: EventBusServiceMock,
})
beforeAll(async () => {
jest.clearAllMocks()
})
it("calls note model functions", async () => {
await noteService.update(IdMap.getId("note"), "new note")
expect(noteRepo.save).toHaveBeenCalledTimes(1)
expect(noteRepo.save).toHaveBeenCalledWith({
...note,
value: "new note",
})
expect(EventBusServiceMock.emit).toHaveBeenCalledTimes(1)
expect(EventBusServiceMock.emit).toHaveBeenCalledWith(
NoteService.Events.UPDATED,
{ id: IdMap.getId("note") }
)
})
})
describe("delete", () => {
const note = { id: IdMap.getId("note") }
const noteRepo = MockRepository({
softRemove: f => Promise.resolve(),
findOne: f => Promise.resolve(note),
})
const noteService = new NoteService({
manager: MockManager,
noteRepository: noteRepo,
eventBusService: EventBusServiceMock,
})
beforeAll(async () => {
jest.clearAllMocks()
})
it("calls note model functions", async () => {
await noteService.delete(IdMap.getId("note"))
expect(noteRepo.softRemove).toHaveBeenCalledTimes(1)
expect(noteRepo.softRemove).toHaveBeenCalledWith(note)
expect(EventBusServiceMock.emit).toHaveBeenCalledTimes(1)
expect(EventBusServiceMock.emit).toHaveBeenCalledWith(
NoteService.Events.DELETED,
{ id: IdMap.getId("note") }
)
})
})
})
+169
View File
@@ -0,0 +1,169 @@
import { MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces"
import _ from "lodash"
import { TransactionManager } from "typeorm"
class NoteService extends BaseService {
static Events = {
CREATED: "note.created",
UPDATED: "note.updated",
DELETED: "note.deleted",
}
constructor({ manager, noteRepository, eventBusService, userService }) {
super()
/** @private @const {EntityManager} */
this.manager_ = manager
/** @private @const {NoteRepository} */
this.noteRepository_ = noteRepository
/** @private @const {EventBus} */
this.eventBus_ = eventBusService
}
/**
* Sets the service's manager to a given transaction manager
* @param {EntityManager} transactionManager - the manager to use
* @return {NoteService} a cloned note service
*/
withTransaction(transactionManager) {
if (!TransactionManager) {
return this
}
const cloned = new NoteService({
manager: transactionManager,
noteRepository: this.noteRepository_,
eventBus: this.eventBus_,
})
cloned.transactionManager_ = transactionManager
return cloned
}
/**
* Retrieves a specific note.
* @param {*} id - the id of the note to retrieve.
* @param {*} config - any options needed to query for the result.
* @returns {Promise} which resolves to the requested note.
*/
async retrieve(id, config = {}) {
const noteRepo = this.manager_.getCustomRepository(this.noteRepository_)
const validatedId = this.validateId_(id)
const query = this.buildQuery_({ id: validatedId }, config)
const note = await noteRepo.findOne(query)
if (!note) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Note with id: ${id} was not found.`
)
}
return note
}
/** Fetches all notes related to the given selector
* @param {Object} selector - the query object for find
* @param {Object} config - the configuration used to find the objects. contains relations, skip, and take.
* @return {Promise<Note[]>} notes related to the given search.
*/
async list(
selector,
config = {
skip: 0,
take: 50,
relations: [],
}
) {
const noteRepo = this.manager_.getCustomRepository(this.noteRepository_)
const query = this.buildQuery_(selector, config)
return noteRepo.find(query)
}
/**
* Creates a note associated with a given author
* @param {object} data - the note to create
* @param {*} config - any configurations if needed, including meta data
* @returns {Promise} resolves to the creation result
*/
async create(data, config = { metadata: {} }) {
const { metadata } = config
const { resource_id, resource_type, value, author_id } = data
return this.atomicPhase_(async manager => {
const noteRepo = manager.getCustomRepository(this.noteRepository_)
const toCreate = {
resource_id,
resource_type,
value,
author_id,
metadata,
}
const note = await noteRepo.create(toCreate)
const result = await noteRepo.save(note)
await this.eventBus_
.withTransaction(manager)
.emit(NoteService.Events.CREATED, { id: result.id })
return result
})
}
/**
* Updates a given note with a new value
* @param {*} noteId - the id of the note to update
* @param {*} value - the new value
* @returns {Promise} resolves to the updated element
*/
async update(noteId, value) {
return this.atomicPhase_(async manager => {
const noteRepo = manager.getCustomRepository(this.noteRepository_)
const note = await this.retrieve(noteId, { relations: ["author"] })
note.value = value
const result = await noteRepo.save(note)
await this.eventBus_
.withTransaction(manager)
.emit(NoteService.Events.UPDATED, { id: result.id })
return result
})
}
/**
* Deletes a given note
* @param {*} noteId - id of the note to delete
* @returns {Promise}
*/
async delete(noteId) {
return this.atomicPhase_(async manager => {
const noteRepo = manager.getCustomRepository(this.noteRepository_)
const note = await this.retrieve(noteId)
await noteRepo.softRemove(note)
await this.eventBus_
.withTransaction(manager)
.emit(NoteService.Events.DELETED, { id: noteId })
return Promise.resolve()
})
}
}
export default NoteService
+1 -1
View File
@@ -41,7 +41,7 @@ class NotificationService extends BaseService {
/**
* Sets the service's manager to a given transaction manager.
* @parma {EntityManager} transactionManager - the manager to use
* @param {EntityManager} transactionManager - the manager to use
* return {NotificationService} a cloned notification service
*/
withTransaction(transactionManager) {