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

[Feature] Módulo de live shop #146

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
shop_ctx: {
api_url: 'https://api-storefront.dchomolog.dooca.store/',
token:
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzaG9wX2lkIjoxMDM3NzksInR5cGUiOiJhZG1pbiIsImVtYWlsIjoic2ltYW8ubWV5cmVyQGJhZ3kuY29tLmJyIiwiZmlyc3RfbmFtZSI6IlNpbVx1MDBlM28iLCJhY3RpdmUiOnRydWUsImlhdCI6MTY5NTE1MjU2NX0.56NBX4cX4-fAt0CehPUNqCSMs3OTa617ImXlyZQ8nRA',
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzaG9wX2lkIjoxMDM3Nzl9.aN9JQNFDsB2bZ56KuTSg9F0G8lmOqK59XEd_VXhaa5M',
domain: 'sapato-do-simon.homolog.bagypro.com',
mock: {}
}
Expand Down
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface shop {
shop?: any
products?: any
user?: any
live_shop?: any
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ProductService } from './modules/product/ProductService'
import { UserService } from './modules/user/UserService'
import { FreightService } from './modules/freight/FreightService'
import { BuyTogetherService } from './modules/buy-together/BuyTogetherService'
import { LiveShopService } from './modules/live-shop/LiveShopService'

import { SeoServiceFactory } from './services/seo/SeoServiceFactory'
import { CookieService } from './services/CookieService'
Expand Down Expand Up @@ -56,5 +57,6 @@ export {
CookieService,
Socket,
NavigationService,
BuyTogetherService
BuyTogetherService,
LiveShopService
}
64 changes: 64 additions & 0 deletions src/mocks/live-shop/live-shop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"id": 1,
"hashRoom": "abc123",
"name": "Super Live Shop",
"slug": "super-live-shop",
"status": "inLive",
"urlLive": "https://www.youtube.com/watch?v=Tii6ljAdSm0",
"title": "Bem vindo a Super Live Shop!",
"banner": {
"src": "https://picsum.photos/200",
"alt": "Banner Image"
},
"products": [
{
"productId": 2915719,
"discount": {
"type": "markup",
"value": 10
},
"variations": [
{
"id": 1001,
"discount": {
"type": "fixed",
"value": 5
}
}
]
},
{
"productId": 2915788,
"discount": {
"type": "markup",
"value": 10
},
"variations": [
{
"id": 1001,
"discount": {
"type": "fixed",
"value": 5
}
}
]
}
],
"messages": [
{
"title": "Boas vindas!",
"content": "Bem vindo a primeira live shop!"
},
{
"title": "Os menores precos!",
"content": "Bem vindo que tá bombando!"
}
],
"alertVisible": true,
"chatVisible": false,
"isActive": true,
"createdAt": "2024-07-03T12:00:00Z",
"updatedAt": "2024-07-03T12:00:00Z"
}
]
68 changes: 68 additions & 0 deletions src/modules/live-shop/LiveShopQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
export class LiveShopQueries {
fields: null | string[]

constructor(fields: string[]) {
this.fields = fields || this.defaultFields()
}

private getImageFields() {
return '{alt, src}'
}

private getMessageFields() {
return '{id, title, content, status}'
}

private getDiscountFields() {
return '{type, value}'
}

private getVariationFields() {
return `
{
variationId,
discount ${this.getDiscountFields()}
}`
}

private getProductFields() {
return `
{
productId,
discount ${this.getDiscountFields()},
variations ${this.getVariationFields()},
status
}`
}

defaultFields() {
return [
'id',
'name',
'status',
'urlLive',
'title',
`banner ${this.getImageFields()}`,
`messages ${this.getMessageFields()}`,
`products ${this.getProductFields()}`,
'alertVisible',
'chatVisible',
'isActive',
'createdAt',
'updatedAt'
]
}

getFields() {
return this.fields.join()
}

getOneFullQuery() {
return `
query LiveShop($filter: FilterLiveShop){
liveShop(filter: $filter){
${this.getFields()}
}
}`
}
}
13 changes: 13 additions & 0 deletions src/modules/live-shop/LiveShopRepositoryGql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getClient } from '../../services/GraphqlService'
import { LiveShopQueries } from './LiveShopQueries'
import { LiveShop, LiveShopFields, LiveShopFilter, LiveShopResponse } from './LiveShopTypes'

export class LiveShopRepositoryGql {
static async getOne(filter: { filter: LiveShopFilter }, fields?: LiveShopFields[]): Promise<LiveShop> {
const liveShopQuery = new LiveShopQueries(fields)
const fullQuery: string = liveShopQuery.getOneFullQuery()
const { liveShop }: LiveShopResponse = await getClient().query(fullQuery, { ...filter })

return liveShop
}
}
17 changes: 17 additions & 0 deletions src/modules/live-shop/LiveShopRepositoryJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { LiveShop, LiveShopFilter } from './LiveShopTypes'

export class LiveShopRepositoryJson {
static async getOne({ filter }: { filter: LiveShopFilter }, fields?: string[]): Promise<LiveShop> {
const liveShop = shop_ctx.mock?.live_shop
const liveShopResult = liveShop.find(
item =>
(filter.hashRoom && item.hashRoom === filter.hashRoom) ||
(filter.id && item.id === filter.id) ||
(filter.slug && item.slug === filter.slug)
)
if (!liveShopResult) {
throw new Error('Live Shop not found')
}
return liveShopResult
}
}
29 changes: 29 additions & 0 deletions src/modules/live-shop/LiveShopService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { BroadcastService } from '../../services/broadcast/broadcast-service'
import { LiveShopRepositoryGql } from './LiveShopRepositoryGql'
import { LiveShopRepositoryJson } from './LiveShopRepositoryJson'
import { LiveShop, LiveShopFields } from './LiveShopTypes'

const Repository = () => (shop_ctx.mock?.live_shop ? LiveShopRepositoryJson : LiveShopRepositoryGql)

export class LiveShopService {
static async getByHash(hashRoom: string, fields?: LiveShopFields[]): Promise<LiveShop> {
try {
const result: LiveShop = await Repository().getOne({ filter: { hashRoom } }, fields)
BroadcastService.emit('LiveShop', result)

return result
} catch (error) {
throw new Error(error?.message)
}
}
static async getById(id: number, fields?: LiveShopFields[]): Promise<LiveShop> {
try {
const result: LiveShop = await Repository().getOne({ filter: { id } }, fields)
BroadcastService.emit('LiveShop', result)

return result
} catch (error) {
throw new Error(error?.message)
}
}
}
71 changes: 71 additions & 0 deletions src/modules/live-shop/LiveShopTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
export type LiveShopItemStatus = 'displaying' | 'hidden' | 'highlighting'

export type LiveShopFields =
| 'id'
| 'hashRoom'
| 'name'
| 'status'
| 'urlLive'
| 'title'
| 'banner'
| 'products'
| 'messages'
| 'alertVisible'
| 'chatVisible'
| 'isActive'
| 'createdAt'
| 'updatedAt'

export interface LiveShopFilter {
id?: number
hashRoom?: string
slug?: string
}
export interface LiveShopImage {
src: string
alt: string
}

export interface LiveShopDiscount {
type: string
value: number
}

export interface LiveShopMessage {
title: string
content: string
status: LiveShopItemStatus
id?: string
}

export interface LiveShopVariation {
id: number
discount: LiveShopDiscount
}
export interface LiveShopProduct {
productId: number
discount: LiveShopDiscount
variations: LiveShopVariation[]
status: LiveShopItemStatus
}

export interface LiveShop {
id: number
hashRoom: string
slug: string
name: string
status: 'inLive' | 'finished' | 'warmup' | string
urlLive: string
title: string
banner: LiveShopImage
products: LiveShopProduct[]
messages: LiveShopMessage[]
alertVisible: boolean
chatVisible: boolean
isActive: boolean
createdAt: string
updatedAt: string
}
export interface LiveShopResponse {
liveShop: LiveShop
}
18 changes: 18 additions & 0 deletions src/modules/live-shop/__test__/live-shop.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'isomorphic-fetch'
import { LiveShop } from '../LiveShopTypes'
import { LiveShopService } from '../LiveShopService'

const HASH_FILTER = 'abc123'
const ID_FILTER = 1

describe('Live Shop Module', () => {
it('Should get live shop by id with all fields successfully', async () => {
const liveShop: LiveShop = await LiveShopService.getById(ID_FILTER)
expect(liveShop.hashRoom).toEqual(HASH_FILTER)
})

it('Should get live shop by hash with all fields successfully', async () => {
const liveShop: LiveShop = await LiveShopService.getByHash(HASH_FILTER)
expect(liveShop.hashRoom).toEqual(HASH_FILTER)
})
})
5 changes: 3 additions & 2 deletions src/modules/product/ProductTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ export interface Aggregator {
}
export interface OptionsGetProductList {
filter: ProductPaginationFilter
fields?: ProductFields[]
fields?: Partial<ProductFields>[]
agg?: Aggregator
}

export interface ProductListFilter extends Omit<ProductPaginationFilter, 'first'> {
export interface ProductListFilter extends ProductPaginationFilter {
items?: number
}

Expand Down Expand Up @@ -288,3 +288,4 @@ export type ProductFields =
| 'variations'
| 'components'
| 'componentGroups'
| string
1 change: 1 addition & 0 deletions src/services/broadcast/broadcast-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type BroadcastEvents =
| 'Category'
| 'Freight'
| 'LandingPages'
| 'LiveShop'
| 'Menu'
| 'Newsletter'
| 'Pages'
Expand Down
Loading