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

[Task] Criar service de live shop #144

Merged
merged 5 commits into from
Jul 5, 2024
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
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
}
44 changes: 44 additions & 0 deletions src/mocks/live-shop/live-shop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"id": 1,
"hashRoom": "abc123",
"name": "Super Live Shop",
"slug": "super-live-shop",
"status": "inLive",
"urlLive": "https://www.youtube.com/watch?v=J-K0cF0BCx4",
"title": "Bem vindo a Super Live Shop!",
"banner": {
"src": "https://picsum.photos/200",
"alt": "Banner Image"
},
"products": [
{
"id": 101,
"discount": {
"type": "markup",
"value": 10
},
"variations": [
{
"id": 1001,
"discount": {
"type": "fixed",
"value": 5
}
}
]
}
],
"messages": [
{
"title": "Boas vindas!",
"content": "Bem vindo a primeira live shop!"
}
],
"alertVisible": true,
"chatVisible": false,
"isActive": true,
"createdAt": "2024-07-03T12:00:00Z",
"updatedAt": "2024-07-03T12:00:00Z"
}
]
67 changes: 67 additions & 0 deletions src/modules/live-shop/LiveShopQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export class LiveShopQueries {
fields: null | string[]

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

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

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

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

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

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

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
}
}
18 changes: 18 additions & 0 deletions src/modules/live-shop/LiveShopRepositoryJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import liveShopJsonMock from '../../mocks/live-shop/live-shop.json'
import { LiveShop, LiveShopFilter } from './LiveShopTypes'

export class LiveShopRepositoryJson {
static async getOne({ filter }: { filter: LiveShopFilter }, fields?: string[]): Promise<LiveShop> {
const liveShop = liveShopJsonMock
const liveShopResult = liveShop.find(
item =>
(filter.hash && item.hashRoom === filter.hash) ||
(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(hash: string, fields?: LiveShopFields[]): Promise<LiveShop> {
try {
const result: LiveShop = await Repository().getOne({ filter: { hash } }, 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)
}
}
}
65 changes: 65 additions & 0 deletions src/modules/live-shop/LiveShopTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export type LiveShopFields =
| 'id'
| 'hashRoom'
| 'name'
| 'status'
| 'urlLive'
| 'title'
| 'banner'
| 'products'
| 'messages'
| 'alertVisible'
| 'chatVisible'
| 'isActive'
| 'createdAt'
| 'updatedAt'

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

export interface LiveShopDiscount {
type: string
value: number
}

export interface LiveShopMessage {
title: string
content: string
}

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

export interface LiveShop {
id: number
hashRoom: 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)
})
})
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