Skip to content

Commit

Permalink
[Seller Quotes] feat: endpoint to save a seller quote (#62)
Browse files Browse the repository at this point in the history
* feat: add new markeplace splitting quotes on graphql

* feat: checking new field quotesManagedBy when value is SELLER

* docs: update CHANGELOG

* feat: add configuration for quote creation

* feat: add adjustment for quote seller

* chore: fix prettier errors

* fix: returning ids separated by commas when multiple quotes

* fix: check config and default settings with marketplace option

* feat: new fields for quotes managed by seller

* feat: new fields to save parent quote; change getQuotes query to filter only parent quotes

* feat: create client to notify seller quote

* feat: add changelog

* feat: verify and notify seller quote

* chore: removes console.log from SellerQuotesClient

* docs: update CHANGELOG and prettier fix on markdown

* fix: right splitting of seller quotes

* feat: refactor on creating seller quote map; avoid double check of seller

* refactor: seller quotes client constants

* feat: send creationDate on notify seller quote

* feat: provides a route for seller get a quote by id at marketplace

* docs: update CHANGELOG

* refactor: separating get seller quote into smaller functions

* refactor: function to get org anc cost center names

* refactor: create service class for seller quotes

* fix: removing approvedBySeller field from quote entity

* refactor: renaming seller quotes service to controller

* feat: provides a route for seller save a quote at marketplace

* feat: get seller quotes paginated

* feat: use seller quote controller

* docs: add changelog

* chore: fix misspell

* fix: use array destructuring

* fix: sorting get seller quotes for right pagination

* feat: supporting search and status filters on get seller quotes

* feat: using search filter on quote creator email

* fix: right dependencies

* fix: remove p-limit from root package.json

* feat: suport for custom sort and where on ger seller quotes paginated

* [Seller Quotes] Add new markeplace splitting quotes on graphql (#57)

* feat: add new markeplace splitting quotes on graphql

* docs: update CHANGELOG

* chore: fix prettier errors

* fix: check config and default settings with marketplace option

* docs: update CHANGELOG and prettier fix on markdown

---------

Co-authored-by: Bruna Santos <[email protected]>
Co-authored-by: Tiago de Andrade Freire <[email protected]>
Co-authored-by: Tiago de Andrade Freire <[email protected]>

---------

Co-authored-by: Bruna Santos <[email protected]>
Co-authored-by: Guido Bernal <[email protected]>
Co-authored-by: BrunaCubos <[email protected]>
  • Loading branch information
4 people authored Jan 10, 2025
1 parent f79bff1 commit 99639f5
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Provides a route for seller get paginated list of quotes at marketplace
- Provides a route for seller save a quote at marketplace

## [2.7.0] - 2025-01-09

Expand Down
4 changes: 4 additions & 0 deletions node/resolvers/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getAppId } from '../../constants'
import { processQueue } from '../../utils/Queue'
import { getSellerQuote } from './seller/getSellerQuote'
import { getSellerQuotesPaginated } from './seller/getSellerQuotesPaginated'
import { saveSellerQuote } from './seller/saveSellerQuote'
import {
setSellerResponseMetadata,
validateSellerRequest,
Expand Down Expand Up @@ -38,4 +39,7 @@ export const Routes = {
getSellerQuotesPaginated: method({
GET: createSellerHandlers(getSellerQuotesPaginated),
}),
saveSellerQuote: method({
POST: createSellerHandlers(saveSellerQuote),
}),
}
26 changes: 26 additions & 0 deletions node/resolvers/routes/seller/saveSellerQuote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { UserInputError } from '@vtex/api'
import { json } from 'co-body'

import { invalidParam } from './utils'

function throwsInputError(): never {
throw new UserInputError('save-seller-quote-invalid-params')
}

export async function saveSellerQuote(ctx: Context, next: NextFn) {
const { id } = ctx.vtex.route.params

if (invalidParam(id)) {
throwsInputError()
}

const payload: Partial<Quote> = await json(ctx.req)

if (!payload) {
throwsInputError()
}

await ctx.vtex.sellerQuotesController?.saveSellerQuote(id, payload)

await next()
}
63 changes: 63 additions & 0 deletions node/resolvers/utils/sellerQuotesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ export default class SellerQuotesController {
return { organizationName, costCenterName }
}

private async getAllChildrenQuotes(parentQuote: string) {
const result: Quote[] = []

const getQuotes = async (page = 1) => {
const quotes = await this.ctx.clients.masterdata.searchDocuments<Quote>({
dataEntity: QUOTE_DATA_ENTITY,
schema: SCHEMA_VERSION,
pagination: { page, pageSize: 100 },
fields: ['subtotal'],
where: `parentQuote=${parentQuote}`,
})

if (quotes.length) {
result.push(...quotes)

await getQuotes(page + 1)
}
}

await getQuotes()

return result
}

public async getFullSellerQuote(id: string) {
const quote = await this.getSellerQuote(id)
const { organizationName, costCenterName } = await this.getOrganizationData(
Expand Down Expand Up @@ -105,4 +129,43 @@ export default class SellerQuotesController {
pagination,
}
}

public async saveSellerQuote(id: string, fields: Partial<Quote>) {
const currentQuote = await this.getSellerQuote(id)

await this.ctx.clients.masterdata.updatePartialDocument({
dataEntity: QUOTE_DATA_ENTITY,
schema: SCHEMA_VERSION,
fields,
id,
})

const { subtotal } = currentQuote
const subtotalDelta = (fields?.subtotal ?? subtotal) - subtotal
const { parentQuote } = fields

if (!subtotalDelta || !parentQuote) return

/**
* The seller can update the subtotal of your quote changing items
* prices. Therefore, it is necessary to update the subtotal of the
* parent quote. The call below is asynchronous as there is no need
* to stop the flow because of this operation.
*/
this.getAllChildrenQuotes(parentQuote)
.then((childrenQuotes) => {
const sumSubtotal = childrenQuotes.reduce(
(acc, quote) => acc + quote.subtotal,
0
)

this.ctx.clients.masterdata.updatePartialDocument({
dataEntity: QUOTE_DATA_ENTITY,
schema: SCHEMA_VERSION,
fields: { subtotal: sumSubtotal },
id: parentQuote,
})
})
.catch(() => null)
}
}
12 changes: 12 additions & 0 deletions node/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
"principals": ["vrn:apps:*:*:*:app/vtex.b2b-seller-quotes@*"]
}
]
},
"saveSellerQuote": {
"path": "/b2b-quotes-graphql/_v/0/save-seller-quote/:seller/:id",
"public": true,
"access": "authorized",
"policies": [
{
"effect": "allow",
"actions": ["post"],
"principals": ["vrn:apps:*:*:*:app/vtex.b2b-seller-quotes@*"]
}
]
}
}
}

0 comments on commit 99639f5

Please sign in to comment.