Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.5.19 #60

Merged
merged 2 commits into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions lib/api/controllers/ProductController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,49 @@ export class ProductController extends Controller {
})
}

/**
*
* @param req
* @param res
*/
updateImage(req, res) {
const ProductService = this.app.services.ProductService
const product = req.params.id
const variant = req.params.variant
const image = req.params.image

ProductService.updateImage(image, req.body)
.then(_image => {
return this.app.services.PermissionsService.sanitizeResult(req, _image)
})
.then(result => {
return res.json(result)
})
.catch(err => {
return res.serverError(err)
})
}

/**
*
* @param req
* @param res
*/
updateImages(req, res) {
const ProductService = this.app.services.ProductService

ProductService.updatesImage(req.body)
.then(_images => {
return this.app.services.PermissionsService.sanitizeResult(req, _images)
})
.then(result => {
return res.json(result)
})
.catch(err => {
return res.serverError(err)
})
}

/**
*
* @param req
Expand All @@ -794,6 +837,11 @@ export class ProductController extends Controller {
const variant = req.params.variant
const image = req.body

if (!image) {
const err = new Error('Image File failed to upload, check input type is file and try again.')
return res.serverError(err)
}

ProductService.addImage(product, variant, image.src, req.body)
.then(_variant => {
return this.app.services.PermissionsService.sanitizeResult(req, _variant)
Expand All @@ -806,6 +854,30 @@ export class ProductController extends Controller {
})
}

/**
*
* @param req
* @param res
*/
updateVariantImage(req, res) {
const ProductService = this.app.services.ProductService

const product = req.params.id
const variant = req.params.variant
const image = req.params.image

ProductService.updateImage(product, variant, image, req.body)
.then(_variant => {
return this.app.services.PermissionsService.sanitizeResult(req, _variant)
})
.then(result => {
return res.json(result)
})
.catch(err => {
return res.serverError(err)
})
}

/**
*
* @param req
Expand Down Expand Up @@ -1123,6 +1195,10 @@ export class ProductController extends Controller {
*/
removeAssociation(req, res) {
const ProductService = this.app.services.ProductService
if (!req.params.id || !req.params.association) {
const err = new ModelError('E_BAD_REQUEST', 'Request missing product or association id')
return res.serverError(err)
}
ProductService.removeAssociation(req.params.id, req.params.association)
.then(product => {
return this.app.services.PermissionsService.sanitizeResult(req, product)
Expand Down
5 changes: 4 additions & 1 deletion lib/api/models/ProductVariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export class ProductVariantResolver extends SequelizeResolver {
* @returns {*|Promise.<Instance>}
*/
findByIdDefault (id, options = {}) {
options = defaultsDeep(options, ProductVariantQuery.default(this.app))
options = this.app.services.SequelizeService.mergeOptionDefaults(
options,
ProductVariantQuery.default(this.app)
)
return this.findById(id, options)
}

Expand Down
97 changes: 86 additions & 11 deletions lib/api/services/ProductService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,11 +1118,16 @@ export class ProductService extends Service {
})
})
.then(() => {
if (options.variant) {
return Variant.findByIdDefault(resDestroy.product_variant_id, { transaction: options.transaction || null })
}
return Product.findByIdDefault(resDestroy.product_id, { transaction: options.transaction || null })
})
return resDestroy
})
// Deprecated in 1.5.19
// https://github.com/fabrix-app/spool-cart/issues/58
// .then(() => {
// if (options.variant) {
// return Variant.findByIdDefault(resDestroy.product_variant_id, { transaction: options.transaction || null })
// }
// return Product.findByIdDefault(resDestroy.product_id, { transaction: options.transaction || null })
// })
}

/**
Expand All @@ -1144,15 +1149,16 @@ export class ProductService extends Service {
throw new Error('Product could not be resolved')
}
resProduct = foundProduct

if (variant) {
return Variant.resolve(variant, { transaction: options.transaction || null })
}
else {
return null
}
})
.then(foundVariant => {
resVariant = foundVariant ? foundVariant.id : null
.then(_variant => {
resVariant = _variant ? _variant.id : null

return resProduct.createImage({
product_variant_id: resVariant,
Expand All @@ -1176,12 +1182,12 @@ export class ProductService extends Service {
transaction: options.transaction || null
})
})
.then(foundImages => {
foundImages = foundImages.map((_image, index) => {
.then(_images => {
_images = _images.map((_image, index) => {
_image.position = index + 1
return _image
})
return Image.sequelize.Promise.mapSeries(foundImages, _image => {
return Image.sequelize.Promise.mapSeries(_images, _image => {
return _image.save({
transaction: options.transaction || null
})
Expand All @@ -1192,6 +1198,72 @@ export class ProductService extends Service {
})
}

/**
*
* @param images
*/
updateImages(images, options: {[key: string]: any} = {}) {
if (!Array.isArray(images)) {
images = [images]
}
const Product = this.app.models['Product']
return Product.sequelize.Promise.mapSeries(images, image => {
return this.updateImage(image, image, options)
})
}

/**
*
* @param image
* @param body
* @param options
*/
updateImage(image, body, options: { [key: string]: any } = {}) {
const Image = this.app.models['ProductImage']
const Product = this.app.models['Product']
const Variant = this.app.models['ProductVariant']

let resUpdate
return Image.resolve(image, {
transaction: options.transaction || null
})
.then(_image => {
if (!_image) {
// TODO proper error
throw new Error('Image not found')
}
resUpdate = _image

return Image.findAll({
where: {
product_id: resUpdate.product_id
},
order: [['position', 'ASC']],
transaction: options.transaction || null
})
})
// .then(_images => {
// _images = _images.filter(image => image.id !== id)
// _images = _images.map((image, index) => {
// image.position = index + 1
// return image
// })
// return Image.sequelize.Promise.mapSeries(_images, image => {
// return image.save({
// transaction: options.transaction || null
// })
// })
// })
.then(updatedImages => {
return resUpdate.update(body, {
transaction: options.transaction || null
})
})
.then(() => {
return resUpdate
})
}

createImage(product, variant, filePath, options: { [key: string]: any } = {}) {
const image = fs.readFileSync(filePath)
const Image = this.app.models['ProductImage']
Expand Down Expand Up @@ -1439,8 +1511,11 @@ export class ProductService extends Service {
return false
})
.then(_newAssociation => {
return Product.findByIdDefault(resProduct.id, { transaction: options.transaction || null })
return resAssociationProduct
})
// .then(_newAssociation => {
// return Product.findByIdDefault(resProduct.id, { transaction: options.transaction || null })
// })
}

/**
Expand Down
101 changes: 101 additions & 0 deletions lib/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7826,6 +7826,7 @@ export const routes = {
}
}
},
// TODO Legacy
'/product/:id/image/:image/add': {
'POST': {
handler: 'ProductController.addImage',
Expand All @@ -7852,6 +7853,7 @@ export const routes = {
}
}
},
// TODO legacy
'/product/:id/image/:image/remove': {
'POST': {
handler: 'ProductController.removeImage',
Expand All @@ -7878,7 +7880,57 @@ export const routes = {
}
}
},
'/product/:id/image': {
'POST': {
handler: 'ProductController.addImage',
config: {
prefix: 'cart.prefix',
validate: {
params: {
id: joi.alternatives().try(
joi.number(),
joi.string()
).required(),
image: joi.alternatives().try(
joi.number(),
joi.string()
).required()
}
},
app: {
permissions: {
resource_name: 'apiPostProductIdImageImageRoute',
roles: ['admin']
}
}
}
}
},
'/product/:id/image/:image': {
'PUT': {
handler: 'ProductController.updateImage',
config: {
prefix: 'cart.prefix',
validate: {
params: {
id: joi.alternatives().try(
joi.number(),
joi.string()
).required(),
image: joi.alternatives().try(
joi.number(),
joi.string()
).required()
}
},
app: {
permissions: {
resource_name: 'apiPutProductIdImageImageRoute',
roles: ['admin']
}
}
}
},
'DELETE': {
handler: 'ProductController.removeImage',
config: {
Expand All @@ -7905,6 +7957,34 @@ export const routes = {
}
},
'/product/:id/variant/:variant/image/:image': {
'PUT': {
handler: 'ProductController.updateVariantImage',
config: {
prefix: 'cart.prefix',
validate: {
params: {
id: joi.alternatives().try(
joi.number(),
joi.string()
).required(),
variant: joi.alternatives().try(
joi.number(),
joi.string()
).required(),
image: joi.alternatives().try(
joi.number(),
joi.string()
).required()
}
},
app: {
permissions: {
resource_name: 'apiPutProductIdVariantVariantImageImageRoute',
roles: ['admin']
}
}
}
},
'DELETE': {
handler: 'ProductController.removeVariantImage',
config: {
Expand Down Expand Up @@ -8178,6 +8258,27 @@ export const routes = {
}
}
}
},
// TODO JOI
'PUT': {
handler: 'ProductController.updateImages',
config: {
prefix: 'cart.prefix',
validate: {
params: {
id: joi.alternatives().try(
joi.number(),
joi.string()
).required()
}
},
app: {
permissions: {
resource_name: 'apiPutProductIdImagesRoute',
roles: ['admin']
}
}
}
}
},
'/product/:id/shops': {
Expand Down
Loading