From d4debaaf20e6ec8452832e55b6091f3561220b67 Mon Sep 17 00:00:00 2001 From: simaomeyrerdooca Date: Thu, 4 Jul 2024 14:02:36 -0300 Subject: [PATCH 1/5] feat: add modulo de liveShop --- src/env.d.ts | 1 + src/index.ts | 4 +- src/modules/live-shop/LiveShopQueries.ts | 67 +++++++++++++++++++ .../live-shop/LiveShopRepositoryGql.ts | 13 ++++ .../live-shop/LiveShopRepositoryJson.ts | 10 +++ src/modules/live-shop/LiveShopService.ts | 22 ++++++ src/modules/live-shop/LiveShopTypes.ts | 60 +++++++++++++++++ src/services/broadcast/broadcast-service.ts | 1 + 8 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 src/modules/live-shop/LiveShopQueries.ts create mode 100644 src/modules/live-shop/LiveShopRepositoryGql.ts create mode 100644 src/modules/live-shop/LiveShopRepositoryJson.ts create mode 100644 src/modules/live-shop/LiveShopService.ts create mode 100644 src/modules/live-shop/LiveShopTypes.ts diff --git a/src/env.d.ts b/src/env.d.ts index 7dd5fd3..d0f08f4 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -28,6 +28,7 @@ interface shop { shop?: any products?: any user?: any + live_shop?: any } } diff --git a/src/index.ts b/src/index.ts index d018dde..1a9d686 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -56,5 +57,6 @@ export { CookieService, Socket, NavigationService, - BuyTogetherService + BuyTogetherService, + LiveShopService } diff --git a/src/modules/live-shop/LiveShopQueries.ts b/src/modules/live-shop/LiveShopQueries.ts new file mode 100644 index 0000000..3276ec2 --- /dev/null +++ b/src/modules/live-shop/LiveShopQueries.ts @@ -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 ` + { + id, + discount ${this.getDiscountFields()} + }` + } + + private getProductFields() { + return ` + { + id, + 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()} + } + }` + } +} diff --git a/src/modules/live-shop/LiveShopRepositoryGql.ts b/src/modules/live-shop/LiveShopRepositoryGql.ts new file mode 100644 index 0000000..f796010 --- /dev/null +++ b/src/modules/live-shop/LiveShopRepositoryGql.ts @@ -0,0 +1,13 @@ +import { getClient } from '../../services/GraphqlService' +import { LiveShopQueries } from './LiveShopQueries' +import { LiveShop, LiveShopFields, LiveShopResponse } from './LiveShopTypes' + +export class LiveShopRepositoryGql { + static async getByHash(hash: string, fields?: LiveShopFields[]): Promise { + const liveShopQuery = new LiveShopQueries(fields) + const fullQuery: string = liveShopQuery.getOneFullQuery() + const { liveShop }: LiveShopResponse = await getClient().query(fullQuery, { filter: { hash } }) + + return liveShop + } +} diff --git a/src/modules/live-shop/LiveShopRepositoryJson.ts b/src/modules/live-shop/LiveShopRepositoryJson.ts new file mode 100644 index 0000000..fdd550d --- /dev/null +++ b/src/modules/live-shop/LiveShopRepositoryJson.ts @@ -0,0 +1,10 @@ +import liveShopJsonMock from '../../mocks/live-shop/live-shop.json' +import { LiveShop } from './LiveShopTypes' + +export class LiveShopRepositoryJson { + static async getByHash(hash: string, fields?: string[]): Promise { + const liveShop = liveShopJsonMock + const result = liveShop.find(item => item.hashRoom === hash) + return result + } +} diff --git a/src/modules/live-shop/LiveShopService.ts b/src/modules/live-shop/LiveShopService.ts new file mode 100644 index 0000000..5515005 --- /dev/null +++ b/src/modules/live-shop/LiveShopService.ts @@ -0,0 +1,22 @@ +import { BroadcastService } from '../../services/broadcast/broadcast-service' +import { LiveShopRepositoryGql } from './LiveShopRepositoryGql' +import { LiveShopRepositoryJson } from './LiveShopRepositoryJson' +import { LiveShop, LiveShopFields } from './LiveShopTypes' +import liveShopMock from '../../mocks/live-shop/live-shop.json' + +shop_ctx.mock.live_shop = liveShopMock + +const Repository = () => (shop_ctx.mock?.live_shop ? LiveShopRepositoryJson : LiveShopRepositoryGql) + +export class LiveShopService { + static async getByHash(hash: string, fields?: LiveShopFields[]): Promise { + try { + const result: LiveShop = await Repository().getByHash(hash, fields) + BroadcastService.emit('LiveShop', result) + + return result + } catch (error) { + throw new Error(error?.message) + } + } +} diff --git a/src/modules/live-shop/LiveShopTypes.ts b/src/modules/live-shop/LiveShopTypes.ts new file mode 100644 index 0000000..e45939b --- /dev/null +++ b/src/modules/live-shop/LiveShopTypes.ts @@ -0,0 +1,60 @@ +export type LiveShopFields = + | 'id' + | 'hashRoom' + | 'name' + | 'status' + | 'urlLive' + | 'title' + | 'banner' + | 'products' + | 'messages' + | 'alertVisible' + | 'chatVisible' + | 'isActive' + | 'createdAt' + | 'updatedAt' + +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: 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 +} diff --git a/src/services/broadcast/broadcast-service.ts b/src/services/broadcast/broadcast-service.ts index 9ae6542..afb8911 100644 --- a/src/services/broadcast/broadcast-service.ts +++ b/src/services/broadcast/broadcast-service.ts @@ -6,6 +6,7 @@ export type BroadcastEvents = | 'Category' | 'Freight' | 'LandingPages' + | 'LiveShop' | 'Menu' | 'Newsletter' | 'Pages' From 851d7b5663ec4d7090abbca99deb87c2a9b86eb6 Mon Sep 17 00:00:00 2001 From: simaomeyrerdooca Date: Thu, 4 Jul 2024 14:02:53 -0300 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20adi=C3=A7=C3=A3o=20dos=20testes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mocks/live-shop/live-shop.json | 43 +++++++++++++++++++ src/modules/live-shop/LiveShopService.ts | 3 -- .../live-shop/__test__/live-shop.test.ts | 12 ++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/mocks/live-shop/live-shop.json create mode 100644 src/modules/live-shop/__test__/live-shop.test.ts diff --git a/src/mocks/live-shop/live-shop.json b/src/mocks/live-shop/live-shop.json new file mode 100644 index 0000000..a6629f1 --- /dev/null +++ b/src/mocks/live-shop/live-shop.json @@ -0,0 +1,43 @@ +[ + { + "id": 1, + "hashRoom": "abc123", + "name": "Super Live Shop", + "status": "active", + "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": "percentage", + "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" + } +] diff --git a/src/modules/live-shop/LiveShopService.ts b/src/modules/live-shop/LiveShopService.ts index 5515005..842c39b 100644 --- a/src/modules/live-shop/LiveShopService.ts +++ b/src/modules/live-shop/LiveShopService.ts @@ -2,9 +2,6 @@ import { BroadcastService } from '../../services/broadcast/broadcast-service' import { LiveShopRepositoryGql } from './LiveShopRepositoryGql' import { LiveShopRepositoryJson } from './LiveShopRepositoryJson' import { LiveShop, LiveShopFields } from './LiveShopTypes' -import liveShopMock from '../../mocks/live-shop/live-shop.json' - -shop_ctx.mock.live_shop = liveShopMock const Repository = () => (shop_ctx.mock?.live_shop ? LiveShopRepositoryJson : LiveShopRepositoryGql) diff --git a/src/modules/live-shop/__test__/live-shop.test.ts b/src/modules/live-shop/__test__/live-shop.test.ts new file mode 100644 index 0000000..c12e856 --- /dev/null +++ b/src/modules/live-shop/__test__/live-shop.test.ts @@ -0,0 +1,12 @@ +import 'isomorphic-fetch' +import { LiveShop } from '../LiveShopTypes' +import { LiveShopService } from '../LiveShopService' + +const HASH_FILTER = 'abc123' + +describe('Live Shop Module', () => { + 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) + }) +}) From 3cbd4e5b35dd1ca34f8c7c7dbca618d7ea67c786 Mon Sep 17 00:00:00 2001 From: simaomeyrerdooca Date: Thu, 4 Jul 2024 15:26:56 -0300 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20adi=C3=A7=C3=A3o=20do=20getOne?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.js | 2 +- src/mocks/live-shop/live-shop.json | 5 +++-- src/modules/live-shop/LiveShopQueries.ts | 4 ++-- src/modules/live-shop/LiveShopRepositoryGql.ts | 7 +++++-- src/modules/live-shop/LiveShopRepositoryJson.ts | 14 +++++++++++--- src/modules/live-shop/LiveShopService.ts | 12 +++++++++++- src/modules/live-shop/LiveShopTypes.ts | 2 +- src/modules/live-shop/__test__/live-shop.test.ts | 6 ++++++ 8 files changed, 40 insertions(+), 12 deletions(-) diff --git a/jest.config.js b/jest.config.js index 0d04200..9b9fc0a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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: {} } diff --git a/src/mocks/live-shop/live-shop.json b/src/mocks/live-shop/live-shop.json index a6629f1..3a038ce 100644 --- a/src/mocks/live-shop/live-shop.json +++ b/src/mocks/live-shop/live-shop.json @@ -3,7 +3,8 @@ "id": 1, "hashRoom": "abc123", "name": "Super Live Shop", - "status": "active", + "slug": "super-live-shop", + "status": "inLive", "urlLive": "https://www.youtube.com/watch?v=J-K0cF0BCx4", "title": "Bem vindo a Super Live Shop!", "banner": { @@ -14,7 +15,7 @@ { "id": 101, "discount": { - "type": "percentage", + "type": "markup", "value": 10 }, "variations": [ diff --git a/src/modules/live-shop/LiveShopQueries.ts b/src/modules/live-shop/LiveShopQueries.ts index 3276ec2..4703985 100644 --- a/src/modules/live-shop/LiveShopQueries.ts +++ b/src/modules/live-shop/LiveShopQueries.ts @@ -20,7 +20,7 @@ export class LiveShopQueries { private getVariationFields() { return ` { - id, + variationId, discount ${this.getDiscountFields()} }` } @@ -28,7 +28,7 @@ export class LiveShopQueries { private getProductFields() { return ` { - id, + productId, discount ${this.getDiscountFields()}, variations ${this.getVariationFields()} }` diff --git a/src/modules/live-shop/LiveShopRepositoryGql.ts b/src/modules/live-shop/LiveShopRepositoryGql.ts index f796010..ec813c7 100644 --- a/src/modules/live-shop/LiveShopRepositoryGql.ts +++ b/src/modules/live-shop/LiveShopRepositoryGql.ts @@ -3,10 +3,13 @@ import { LiveShopQueries } from './LiveShopQueries' import { LiveShop, LiveShopFields, LiveShopResponse } from './LiveShopTypes' export class LiveShopRepositoryGql { - static async getByHash(hash: string, fields?: LiveShopFields[]): Promise { + static async getOne( + filter: { filter: { id?: number; hash?: string; slug?: string } }, + fields?: LiveShopFields[] + ): Promise { const liveShopQuery = new LiveShopQueries(fields) const fullQuery: string = liveShopQuery.getOneFullQuery() - const { liveShop }: LiveShopResponse = await getClient().query(fullQuery, { filter: { hash } }) + const { liveShop }: LiveShopResponse = await getClient().query(fullQuery, { ...filter }) return liveShop } diff --git a/src/modules/live-shop/LiveShopRepositoryJson.ts b/src/modules/live-shop/LiveShopRepositoryJson.ts index fdd550d..c095d82 100644 --- a/src/modules/live-shop/LiveShopRepositoryJson.ts +++ b/src/modules/live-shop/LiveShopRepositoryJson.ts @@ -2,9 +2,17 @@ import liveShopJsonMock from '../../mocks/live-shop/live-shop.json' import { LiveShop } from './LiveShopTypes' export class LiveShopRepositoryJson { - static async getByHash(hash: string, fields?: string[]): Promise { + static async getOne(filter: { id?: number; hash?: string; slug?: string }, fields?: string[]): Promise { const liveShop = liveShopJsonMock - const result = liveShop.find(item => item.hashRoom === hash) - return result + 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 } } diff --git a/src/modules/live-shop/LiveShopService.ts b/src/modules/live-shop/LiveShopService.ts index 842c39b..7a977a1 100644 --- a/src/modules/live-shop/LiveShopService.ts +++ b/src/modules/live-shop/LiveShopService.ts @@ -8,7 +8,17 @@ const Repository = () => (shop_ctx.mock?.live_shop ? LiveShopRepositoryJson : Li export class LiveShopService { static async getByHash(hash: string, fields?: LiveShopFields[]): Promise { try { - const result: LiveShop = await Repository().getByHash(hash, fields) + 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 { + try { + const result: LiveShop = await Repository().getOne({ filter: { id } }, fields) BroadcastService.emit('LiveShop', result) return result diff --git a/src/modules/live-shop/LiveShopTypes.ts b/src/modules/live-shop/LiveShopTypes.ts index e45939b..76cd6e3 100644 --- a/src/modules/live-shop/LiveShopTypes.ts +++ b/src/modules/live-shop/LiveShopTypes.ts @@ -43,7 +43,7 @@ export interface LiveShop { id: number hashRoom: string name: string - status: string + status: 'inLive' | 'finished' | 'warmup' | string urlLive: string title: string banner: LiveShopImage diff --git a/src/modules/live-shop/__test__/live-shop.test.ts b/src/modules/live-shop/__test__/live-shop.test.ts index c12e856..2e8b331 100644 --- a/src/modules/live-shop/__test__/live-shop.test.ts +++ b/src/modules/live-shop/__test__/live-shop.test.ts @@ -3,8 +3,14 @@ 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) From 6cf02b809b19242d97077db1f54bd67e20a21160 Mon Sep 17 00:00:00 2001 From: simaomeyrerdooca Date: Fri, 5 Jul 2024 15:41:00 -0300 Subject: [PATCH 4/5] refactor(live-shop): abstrai type --- src/modules/live-shop/LiveShopRepositoryGql.ts | 7 ++----- src/modules/live-shop/LiveShopTypes.ts | 5 +++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/modules/live-shop/LiveShopRepositoryGql.ts b/src/modules/live-shop/LiveShopRepositoryGql.ts index ec813c7..4d68d69 100644 --- a/src/modules/live-shop/LiveShopRepositoryGql.ts +++ b/src/modules/live-shop/LiveShopRepositoryGql.ts @@ -1,12 +1,9 @@ import { getClient } from '../../services/GraphqlService' import { LiveShopQueries } from './LiveShopQueries' -import { LiveShop, LiveShopFields, LiveShopResponse } from './LiveShopTypes' +import { LiveShop, LiveShopFields, LiveShopFilter, LiveShopResponse } from './LiveShopTypes' export class LiveShopRepositoryGql { - static async getOne( - filter: { filter: { id?: number; hash?: string; slug?: string } }, - fields?: LiveShopFields[] - ): Promise { + static async getOne(filter: { filter: LiveShopFilter }, fields?: LiveShopFields[]): Promise { const liveShopQuery = new LiveShopQueries(fields) const fullQuery: string = liveShopQuery.getOneFullQuery() const { liveShop }: LiveShopResponse = await getClient().query(fullQuery, { ...filter }) diff --git a/src/modules/live-shop/LiveShopTypes.ts b/src/modules/live-shop/LiveShopTypes.ts index 76cd6e3..e067339 100644 --- a/src/modules/live-shop/LiveShopTypes.ts +++ b/src/modules/live-shop/LiveShopTypes.ts @@ -14,6 +14,11 @@ export type LiveShopFields = | 'createdAt' | 'updatedAt' +export interface LiveShopFilter { + id?: number + hash?: string + slug?: string +} export interface LiveShopImage { src: string alt: string From 62ceefc5523b23998e77b8f2b890195aae53996d Mon Sep 17 00:00:00 2001 From: simaomeyrerdooca Date: Fri, 5 Jul 2024 15:49:00 -0300 Subject: [PATCH 5/5] refactor: filter no json repo --- src/modules/live-shop/LiveShopRepositoryJson.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/live-shop/LiveShopRepositoryJson.ts b/src/modules/live-shop/LiveShopRepositoryJson.ts index c095d82..d592c5d 100644 --- a/src/modules/live-shop/LiveShopRepositoryJson.ts +++ b/src/modules/live-shop/LiveShopRepositoryJson.ts @@ -1,8 +1,8 @@ import liveShopJsonMock from '../../mocks/live-shop/live-shop.json' -import { LiveShop } from './LiveShopTypes' +import { LiveShop, LiveShopFilter } from './LiveShopTypes' export class LiveShopRepositoryJson { - static async getOne(filter: { id?: number; hash?: string; slug?: string }, fields?: string[]): Promise { + static async getOne({ filter }: { filter: LiveShopFilter }, fields?: string[]): Promise { const liveShop = liveShopJsonMock const liveShopResult = liveShop.find( item =>