-
Notifications
You must be signed in to change notification settings - Fork 113
feat: campaign entity and its queries #2969
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7340dcd
feat: campaign entity
sshanzel 013be8b
feat: campaign queries
sshanzel d2d585f
test: queries
sshanzel 8ca20f0
Update src/entity/campaign/Campaign.ts
sshanzel e4caa81
refactor: partial
sshanzel 32de0fb
refactor: use get limit
sshanzel f7bd871
refactor: usage of built in decorator
sshanzel cffc1e6
refactor: entity
sshanzel b5fc936
refactor: campaign entity
sshanzel d47d9df
Merge branch 'main' into MI-967
sshanzel 249f98d
refactor: creation of index
sshanzel 2f374a4
refactor: migration if not exists
sshanzel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { | ||
| Column, | ||
| CreateDateColumn, | ||
| Entity, | ||
| Index, | ||
| ManyToOne, | ||
| PrimaryGeneratedColumn, | ||
| TableInheritance, | ||
| UpdateDateColumn, | ||
| } from 'typeorm'; | ||
| import type { User } from '../user'; | ||
|
|
||
| export enum CampaignType { | ||
| Post = 'post', | ||
| Source = 'source', | ||
| } | ||
|
|
||
| export enum CampaignState { | ||
| Pending = 'pending', | ||
| Active = 'active', | ||
| Completed = 'completed', | ||
| Cancelled = 'cancelled', | ||
| } | ||
|
|
||
| export interface CampaignFlags { | ||
| budget: number; | ||
| spend: number; | ||
| impressions: number; | ||
| clicks: number; | ||
| users: number; | ||
| } | ||
|
|
||
| @Entity() | ||
| @Index('IDX_campaign_state_created_at_sort', { synchronize: false }) | ||
| @TableInheritance({ column: { type: 'varchar', name: 'type' } }) | ||
| export class Campaign { | ||
| @PrimaryGeneratedColumn('uuid') | ||
| id: string; | ||
|
|
||
| @Column({ type: 'text' }) | ||
| referenceId: string; | ||
|
|
||
| @Column({ type: 'text' }) | ||
| userId: string; | ||
|
|
||
| @ManyToOne('User', { | ||
| lazy: true, | ||
| onDelete: 'CASCADE', | ||
| }) | ||
| user: Promise<User>; | ||
|
|
||
| @Column({ type: 'text' }) | ||
| @Index('IDX_campaign_type') | ||
| type: CampaignType; | ||
|
|
||
| @CreateDateColumn() | ||
| createdAt: Date; | ||
|
|
||
| @UpdateDateColumn() | ||
| updatedAt: Date; | ||
|
|
||
| @Column() | ||
| endedAt: Date; | ||
|
|
||
| @Column({ type: 'text' }) | ||
| state: CampaignState; | ||
|
|
||
| @Column({ type: 'jsonb', default: {} }) | ||
| flags: Partial<CampaignFlags>; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { ChildEntity, Column, ManyToOne } from 'typeorm'; | ||
| import type { Post } from '../posts'; | ||
| import { Campaign, CampaignType } from './Campaign'; | ||
|
|
||
| @ChildEntity(CampaignType.Post) | ||
| export class CampaignPost extends Campaign { | ||
| @Column({ type: 'text', default: null }) | ||
| postId: string; | ||
|
|
||
| @ManyToOne('Post', { lazy: true, onDelete: 'CASCADE' }) | ||
| post: Promise<Post>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { ChildEntity, Column, ManyToOne } from 'typeorm'; | ||
| import type { Source } from '../'; | ||
| import { Campaign, CampaignType } from './Campaign'; | ||
|
|
||
| @ChildEntity(CampaignType.Source) | ||
| export class CampaignSource extends Campaign { | ||
| @Column({ type: 'text', default: null }) | ||
| sourceId: string; | ||
|
|
||
| @ManyToOne('Source', { lazy: true, onDelete: 'CASCADE' }) | ||
| source: Promise<Source>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export * from './Campaign'; | ||
| export * from './CampaignPost'; | ||
| export * from './CampaignSource'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class CampaignEntity1754650534998 implements MigrationInterface { | ||
| name = 'CampaignEntity1754650534998'; | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE IF NOT EXISTS "campaign" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "referenceId" text NOT NULL, "userId" character varying NOT NULL, "type" text NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "endedAt" TIMESTAMP NOT NULL, "state" text NOT NULL, "flags" jsonb NOT NULL DEFAULT '{}', "postId" text, "sourceId" text, CONSTRAINT "PK_0ce34d26e7f2eb316a3a592cdc4" PRIMARY KEY ("id"))`, | ||
| ); | ||
| await queryRunner.query( | ||
| `CREATE INDEX IF NOT EXISTS "IDX_campaign_type" ON "campaign" ("type") `, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "campaign" ADD CONSTRAINT "FK_8e2dc400e55e237feba0869bc02" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "campaign" ADD CONSTRAINT "FK_9074c52d57e727dda6591943b10" FOREIGN KEY ("postId") REFERENCES "post"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "campaign" ADD CONSTRAINT "FK_a8102fd41bef084f19474e97953" FOREIGN KEY ("sourceId") REFERENCES "source"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
| ); | ||
| await queryRunner.query( | ||
| `CREATE INDEX IF NOT EXISTS "IDX_campaign_state_created_at_sort" ON "campaign" ((CASE WHEN state = 'active' THEN 0 ELSE 1 END), "createdAt" DESC) `, | ||
| ); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `DROP INDEX IF EXISTS "public"."IDX_campaign_state_created_at_sort"`, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "campaign" DROP CONSTRAINT "FK_a8102fd41bef084f19474e97953"`, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "campaign" DROP CONSTRAINT "FK_9074c52d57e727dda6591943b10"`, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "campaign" DROP CONSTRAINT "FK_8e2dc400e55e237feba0869bc02"`, | ||
| ); | ||
| await queryRunner.query( | ||
| `DROP INDEX IF EXISTS "public"."IDX_campaign_type"`, | ||
| ); | ||
| await queryRunner.query(`DROP TABLE IF EXISTS "campaign"`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { | ||
| cursorToOffset, | ||
| offsetToCursor, | ||
| type Connection, | ||
| type ConnectionArguments, | ||
| } from 'graphql-relay'; | ||
| import { IResolvers } from '@graphql-tools/utils'; | ||
| import { AuthContext, BaseContext, Context } from '../Context'; | ||
| import { traceResolvers } from './trace'; | ||
|
|
||
| import graphorm from '../graphorm'; | ||
| import { CampaignState, type Campaign } from '../entity/campaign'; | ||
| import type { GQLPost } from './posts'; | ||
| import type { GQLSource } from './sources'; | ||
| import { getLimit } from '../common'; | ||
|
|
||
| interface GQLCampaign | ||
| extends Pick< | ||
| Campaign, | ||
| 'id' | 'type' | 'flags' | 'createdAt' | 'endedAt' | 'referenceId' | 'state' | ||
| > { | ||
| post: GQLPost; | ||
| source: GQLSource; | ||
| } | ||
|
|
||
| export const typeDefs = /* GraphQL */ ` | ||
| type CampaignFlags { | ||
| budget: Int! | ||
| spend: Int! | ||
| users: Int! | ||
| clicks: Int! | ||
| impressions: Int! | ||
| } | ||
|
|
||
| type Campaign { | ||
| id: String! | ||
| type: String! | ||
| state: String! | ||
| createdAt: DateTime! | ||
| endedAt: DateTime! | ||
| flags: CampaignFlags! | ||
| post: Post | ||
| source: Source | ||
| } | ||
|
|
||
| type CampaignEdge { | ||
| node: Campaign! | ||
| """ | ||
| Used in before and after args | ||
| """ | ||
| cursor: String! | ||
| } | ||
|
|
||
| type CampaignConnection { | ||
| pageInfo: PageInfo! | ||
| edges: [CampaignEdge]! | ||
| } | ||
|
|
||
| extend type Query { | ||
| campaignById( | ||
| """ | ||
| ID of the campaign to fetch | ||
| """ | ||
| id: ID! | ||
| ): Campaign! @auth | ||
|
|
||
| campaignsList( | ||
| """ | ||
| Paginate after opaque cursor | ||
| """ | ||
| after: String | ||
| """ | ||
| Paginate first | ||
| """ | ||
| first: Int | ||
| ): CampaignConnection! @auth | ||
| } | ||
| `; | ||
|
|
||
| export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers< | ||
| unknown, | ||
| BaseContext | ||
| >({ | ||
| Query: { | ||
| campaignById: async ( | ||
| _, | ||
| { id }: { id: string }, | ||
| ctx: Context, | ||
| info, | ||
| ): Promise<GQLCampaign> => | ||
| graphorm.queryOneOrFail(ctx, info, (builder) => { | ||
| builder.queryBuilder.where({ id }).andWhere({ userId: ctx.userId }); | ||
|
|
||
| return builder; | ||
| }), | ||
| campaignsList: async ( | ||
| _, | ||
| args: ConnectionArguments, | ||
| ctx: AuthContext, | ||
| info, | ||
| ): Promise<Connection<GQLCampaign>> => { | ||
| const { userId } = ctx; | ||
| const { after, first = 20 } = args; | ||
| const offset = after ? cursorToOffset(after) : 0; | ||
|
|
||
| return graphorm.queryPaginated( | ||
| ctx, | ||
| info, | ||
| () => !!after, | ||
| (nodeSize) => nodeSize === first, | ||
| (_, i) => offsetToCursor(offset + i + 1), | ||
| (builder) => { | ||
| const { alias } = builder; | ||
|
|
||
| builder.queryBuilder.andWhere(`"${alias}"."userId" = :userId`, { | ||
| userId, | ||
| }); | ||
|
|
||
| builder.queryBuilder.orderBy( | ||
| `CASE WHEN "${alias}"."state" = '${CampaignState.Active}' THEN 0 ELSE 1 END`, | ||
| ); | ||
| builder.queryBuilder.addOrderBy(`"${alias}"."createdAt"`, 'DESC'); | ||
| builder.queryBuilder.limit(getLimit({ limit: first ?? 20 })); | ||
|
|
||
| if (after) { | ||
| builder.queryBuilder.offset(offset); | ||
| } | ||
|
|
||
| return builder; | ||
| }, | ||
| ); | ||
| }, | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.