feat(medusa): Migrate Note service (#1925)
**What** Migrate note service to ts FIXES CORE-352
This commit is contained in:
@@ -1,171 +0,0 @@
|
|||||||
import { MedusaError } from "medusa-core-utils"
|
|
||||||
import { BaseService } from "medusa-interfaces"
|
|
||||||
import { TransactionManager } from "typeorm"
|
|
||||||
|
|
||||||
class NoteService extends BaseService {
|
|
||||||
static Events = {
|
|
||||||
CREATED: "note.created",
|
|
||||||
UPDATED: "note.updated",
|
|
||||||
DELETED: "note.deleted",
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor({ manager, noteRepository, eventBusService }) {
|
|
||||||
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_,
|
|
||||||
eventBusService: this.eventBus_,
|
|
||||||
})
|
|
||||||
|
|
||||||
cloned.transactionManager_ = transactionManager
|
|
||||||
return cloned
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a specific note.
|
|
||||||
* @param {string} id - the id of the note to retrieve.
|
|
||||||
* @param {object} config - any options needed to query for the result.
|
|
||||||
* @return {Promise<Note>} 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.
|
|
||||||
* @param {string[]} config.relations - Which relations to include in the resulting list of Notes.
|
|
||||||
* @param {number} config.take - How many Notes to take in the resulting list of Notes.
|
|
||||||
* @param {number} config.skip - How many Notes to skip in the resulting list of Notes.
|
|
||||||
* @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 {CreateNoteInput} data - the note to create
|
|
||||||
* @param {*} config - any configurations if needed, including meta data
|
|
||||||
* @return {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
|
|
||||||
* @return {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
|
|
||||||
* @return {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
|
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
import { MedusaError } from "medusa-core-utils"
|
||||||
|
import { EntityManager } from "typeorm"
|
||||||
|
import { TransactionBaseService } from "../interfaces"
|
||||||
|
import { NoteRepository } from "../repositories/note"
|
||||||
|
import EventBusService from "./event-bus"
|
||||||
|
import { FindConfig, Selector } from "../types/common"
|
||||||
|
import { Note } from "../models"
|
||||||
|
import { buildQuery } from "../utils"
|
||||||
|
import { CreateNoteInput } from "../types/note"
|
||||||
|
|
||||||
|
type InjectedDependencies = {
|
||||||
|
manager: EntityManager
|
||||||
|
noteRepository: typeof NoteRepository
|
||||||
|
eventBusService: EventBusService
|
||||||
|
}
|
||||||
|
|
||||||
|
class NoteService extends TransactionBaseService<NoteService> {
|
||||||
|
static readonly Events = {
|
||||||
|
CREATED: "note.created",
|
||||||
|
UPDATED: "note.updated",
|
||||||
|
DELETED: "note.deleted",
|
||||||
|
}
|
||||||
|
|
||||||
|
protected manager_: EntityManager
|
||||||
|
protected transactionManager_: EntityManager | undefined
|
||||||
|
protected readonly noteRepository_: typeof NoteRepository
|
||||||
|
protected readonly eventBus_: EventBusService
|
||||||
|
|
||||||
|
constructor({
|
||||||
|
manager,
|
||||||
|
noteRepository,
|
||||||
|
eventBusService,
|
||||||
|
}: InjectedDependencies) {
|
||||||
|
super({ manager, noteRepository, eventBusService })
|
||||||
|
|
||||||
|
this.manager_ = manager
|
||||||
|
this.noteRepository_ = noteRepository
|
||||||
|
this.eventBus_ = eventBusService
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a specific note.
|
||||||
|
* @param id - the id of the note to retrieve.
|
||||||
|
* @param config - any options needed to query for the result.
|
||||||
|
* @return which resolves to the requested note.
|
||||||
|
*/
|
||||||
|
async retrieve(
|
||||||
|
id: string,
|
||||||
|
config: FindConfig<Note> = {}
|
||||||
|
): Promise<Note | never> {
|
||||||
|
const noteRepo = this.manager_.getCustomRepository(this.noteRepository_)
|
||||||
|
|
||||||
|
const query = buildQuery({ id }, 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 selector - the query object for find
|
||||||
|
* @param config - the configuration used to find the objects. contains relations, skip, and take.
|
||||||
|
* @param config.relations - Which relations to include in the resulting list of Notes.
|
||||||
|
* @param config.take - How many Notes to take in the resulting list of Notes.
|
||||||
|
* @param config.skip - How many Notes to skip in the resulting list of Notes.
|
||||||
|
* @return notes related to the given search.
|
||||||
|
*/
|
||||||
|
async list(
|
||||||
|
selector: Selector<Note>,
|
||||||
|
config: FindConfig<Note> = {
|
||||||
|
skip: 0,
|
||||||
|
take: 50,
|
||||||
|
relations: [],
|
||||||
|
}
|
||||||
|
): Promise<Note[]> {
|
||||||
|
const noteRepo = this.manager_.getCustomRepository(this.noteRepository_)
|
||||||
|
|
||||||
|
const query = buildQuery(selector, config)
|
||||||
|
|
||||||
|
return noteRepo.find(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a note associated with a given author
|
||||||
|
* @param data - the note to create
|
||||||
|
* @param config - any configurations if needed, including meta data
|
||||||
|
* @return resolves to the creation result
|
||||||
|
*/
|
||||||
|
async create(
|
||||||
|
data: CreateNoteInput,
|
||||||
|
config: { metadata: Record<string, unknown> } = { metadata: {} }
|
||||||
|
): Promise<Note> {
|
||||||
|
const { metadata } = config
|
||||||
|
|
||||||
|
const { resource_id, resource_type, value, author_id } = data
|
||||||
|
|
||||||
|
return await 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
|
||||||
|
* @return resolves to the updated element
|
||||||
|
*/
|
||||||
|
async update(noteId: string, value: string): Promise<Note> {
|
||||||
|
return await 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
|
||||||
|
*/
|
||||||
|
async delete(noteId: string): Promise<void> {
|
||||||
|
return await 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 })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default NoteService
|
||||||
Reference in New Issue
Block a user