Untitled

 avatar
unknown
plain_text
2 years ago
1.9 kB
2
Indexable
func (s *productService) appendMicroSurveysToProducts(
	_ context.Context,
	products []dtos.Product,
	microSurveys []cms.PLPMicroSurveyEntrypointItem,
	currPage, pageSz int,
) []dtos.Product {
	var (
		m                  = map[int]cms.PLPMicroSurveyEntrypointItem{}
		microSurveysSorted []cms.PLPMicroSurveyEntrypointItem
	)
	for _, ms := range microSurveys {
		var (
			idx = ms.Position - 1
		)
		if _, ok := m[idx]; !ok && idx/pageSz == (currPage-1) {
			var (
				idxInCurrPage = idx % pageSz
			)
			ms.Position = idxInCurrPage + 1
			m[idxInCurrPage] = ms
			microSurveysSorted = append(microSurveysSorted, ms)
		}
	}
	if len(m) == 0 {
		return products
	}
	sort.Slice(microSurveysSorted, func(i, j int) bool {
		return microSurveysSorted[i].Position < microSurveysSorted[j].Position
	})

	var (
		result = products
	)
	for _, ms := range microSurveysSorted {
		for i := range result {
			if i != ms.Position-1 {
				continue
			}

			result = append(result[:i+1], result[i:]...)

			var (
				sliderItems []dtos.MicroSurveyPLPEntrypoint
			)
			for _, si := range ms.SliderItems {
				sliderItems = append(sliderItems, dtos.MicroSurveyPLPEntrypoint{
					CTATitle: si.CTATitle,
					Deeplink: si.Deeplink,
					ImageURL: si.ImageURL,
				})
			}
			microSurvey := dtos.MicroSurveyPLPEntrypoint{
				CTATitle:     ms.CTATitle,
				Deeplink:     ms.Deeplink,
				DisplayType:  ms.DisplayType,
				ImageURL:     ms.ImageURL,
				QuestionCode: ms.QuestionCode,
				Ratio:        ms.Ratio,
				SectionCode:  ms.SectionCode,
				Title:        ms.Title,
				SliderItems:  sliderItems,
				Type:         dtos.MicroSurveyTypeItemCard,
			}
			if microSurvey.SectionCode != "" || microSurvey.QuestionCode != "" {
				microSurvey.Type = dtos.MicroSurveyTypeMicroSurvey
			}

			result[i] = dtos.Product{
				Type:        dtos.ProductTypeMicroSurvey,
				MicroSurvey: &microSurvey,
			}
		}
	}

	return result
}
Editor is loading...