Skip to content

Commit

Permalink
PalisadoesFoundation#1415 feature request deleting advertisements (Pa…
Browse files Browse the repository at this point in the history
…lisadoesFoundation#1416)

* Add: Advert model

* Add: Advert model

* Add: graphql types

* Add: createAd, removeAd, getAllAds

* 2

* 2

* Ad: 3

* Added: Typedefs

* Test: removeadvertisement

* test: get advertisement

* fix

* fix: errors

* updated add types

* Added Delete Advertisement Mutations

* fix

* Add: tests

* Add: tests w

* fixed: tests
  • Loading branch information
SiddheshKukade authored Oct 28, 2023
1 parent 9e4d2fc commit e0db32f
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 7 deletions.
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ type Mutation {
createPost(data: PostInput!, file: String): Post
createTask(data: TaskInput!, eventProjectId: ID!): Task!
createUserTag(input: CreateUserTagInput!): UserTag
deleteAdvertisementById(id: ID!): DeletePayload!
deleteDonationById(id: ID!): DeletePayload!
forgotPassword(data: ForgotPasswordData!): Boolean!
joinPublicOrganization(organizationId: ID!): User!
Expand Down
4 changes: 2 additions & 2 deletions src/models/Advertisement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ const advertisementSchema = new Schema({
required: true,
},
startDate: {
type: String,
type: Date,
required: true,
},
endDate: {
type: String,
type: Date,
required: true,
},
});
Expand Down
5 changes: 0 additions & 5 deletions src/resolvers/Mutation/createAdvertisement.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { Advertisement } from "../../models";
//eslint-disable-next-line @typescript-eslint/naming-convention
const { ObjectId } = require("mongodb");
// @ts-ignore
export const createAdvertisement: MutationResolvers["createAdvertisement"] =
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async (_parent, args, _context) => {
// Creates new Ad.
args.orgId = ObjectId(args.orgId);
args.startDate = new Date(args.startDate);
args.endDate = new Date(args.endDate);
const createdAd = await Advertisement.create({
...args,
});
Expand Down
17 changes: 17 additions & 0 deletions src/resolvers/Mutation/deleteAdvertisementById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { Advertisement } from "../../models";

/**
* This function enables to delete a donation record from the database.
* @param _parent - parent of current request
* @param args - payload provided with the request
* @returns Boolean value denoting whether the deletion was successful or not.
*/
export const deleteAdvertisementById: MutationResolvers["deleteAdvertisementById"] =
async (_parent: any, args: { id: any }) => {
const deletedAdvertisement = await Advertisement.deleteOne({
_id: args.id,
});

return { success: deletedAdvertisement.deletedCount ? true : false };
};
2 changes: 2 additions & 0 deletions src/resolvers/Mutation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import { updateUserProfile } from "./updateUserProfile";
import { updateUserPassword } from "./updateUserPassword";
import { updateUserTag } from "./updateUserTag";
import { updateUserType } from "./updateUserType";
import { deleteAdvertisementById } from "./deleteAdvertisementById";

export const Mutation: MutationResolvers = {
acceptAdmin,
Expand Down Expand Up @@ -118,6 +119,7 @@ export const Mutation: MutationResolvers = {
createTask,
createUserTag,
deleteDonationById,
deleteAdvertisementById,
forgotPassword,
joinPublicOrganization,
leaveOrganization,
Expand Down
2 changes: 2 additions & 0 deletions src/typeDefs/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export const mutations = gql`
createTask(data: TaskInput!, eventProjectId: ID!): Task! @auth
deleteAdvertisementById(id: ID!): DeletePayload!
deleteDonationById(id: ID!): DeletePayload!
forgotPassword(data: ForgotPasswordData!): Boolean!
Expand Down
7 changes: 7 additions & 0 deletions src/types/generatedGraphQLTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ export type Mutation = {
createPost?: Maybe<Post>;
createTask: Task;
createUserTag?: Maybe<UserTag>;
deleteAdvertisementById: DeletePayload;
deleteDonationById: DeletePayload;
forgotPassword: Scalars['Boolean'];
joinPublicOrganization: User;
Expand Down Expand Up @@ -747,6 +748,11 @@ export type MutationCreateUserTagArgs = {
};


export type MutationDeleteAdvertisementByIdArgs = {
id: Scalars['ID'];
};


export type MutationDeleteDonationByIdArgs = {
id: Scalars['ID'];
};
Expand Down Expand Up @@ -2439,6 +2445,7 @@ export type MutationResolvers<ContextType = any, ParentType extends ResolversPar
createPost?: Resolver<Maybe<ResolversTypes['Post']>, ParentType, ContextType, RequireFields<MutationCreatePostArgs, 'data'>>;
createTask?: Resolver<ResolversTypes['Task'], ParentType, ContextType, RequireFields<MutationCreateTaskArgs, 'data' | 'eventProjectId'>>;
createUserTag?: Resolver<Maybe<ResolversTypes['UserTag']>, ParentType, ContextType, RequireFields<MutationCreateUserTagArgs, 'input'>>;
deleteAdvertisementById?: Resolver<ResolversTypes['DeletePayload'], ParentType, ContextType, RequireFields<MutationDeleteAdvertisementByIdArgs, 'id'>>;
deleteDonationById?: Resolver<ResolversTypes['DeletePayload'], ParentType, ContextType, RequireFields<MutationDeleteDonationByIdArgs, 'id'>>;
forgotPassword?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType, RequireFields<MutationForgotPasswordArgs, 'data'>>;
joinPublicOrganization?: Resolver<ResolversTypes['User'], ParentType, ContextType, RequireFields<MutationJoinPublicOrganizationArgs, 'organizationId'>>;
Expand Down
70 changes: 70 additions & 0 deletions tests/resolvers/Mutation/deleteAdvertisementById.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import "dotenv/config";
import type { Document } from "mongoose";
import type mongoose from "mongoose";
import { Types } from "mongoose";
import type {
InterfaceAdvertisement,
InterfaceDonation,
} from "../../../src/models";
import { Advertisement } from "../../../src/models";
import type { MutationDeleteDonationByIdArgs } from "../../../src/types/generatedGraphQLTypes";
import { connect, disconnect } from "../../helpers/db";
import { beforeAll, afterAll, describe, it, expect } from "vitest";
import { createTestUserAndOrganization } from "../../helpers/userAndOrg";
import { deleteAdvertisementById } from "../../../src/resolvers/Mutation/deleteAdvertisementById";

let testAdvertisement: InterfaceAdvertisement &
Document<any, any, InterfaceDonation>;
let MONGOOSE_INSTANCE: typeof mongoose;

beforeAll(async () => {
MONGOOSE_INSTANCE = await connect();
const temp = await createTestUserAndOrganization();
const testOrganization = temp[1];
testAdvertisement = await Advertisement.create({
orgId: testOrganization?._id,
endDate: new Date(),
link: "http://example.com",
startDate: new Date(),
type: "POPUP",
name: "Cookies at just $5 for a packet",
});
});

afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

describe("resolvers -> Mutation -> deleteAdvertiementById", () => {
it(`returns false if deletion of advertisement was unsuccessful`, async () => {
const args: MutationDeleteDonationByIdArgs = {
id: Types.ObjectId().toString(),
};

const deleteDonationByIdPayload = await deleteAdvertisementById?.(
{},
args,
{}
);

expect(deleteDonationByIdPayload).toEqual({
success: false,
});
});

it(`returns true if deletion of ads was successful`, async () => {
const args: MutationDeleteDonationByIdArgs = {
id: testAdvertisement._id,
};

const deleteDonationByIdPayload = await deleteAdvertisementById?.(
{},
args,
{}
);

expect(deleteDonationByIdPayload).toEqual({
success: true,
});
});
});
28 changes: 28 additions & 0 deletions tests/resolvers/Query/getAdvertisements.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "dotenv/config";
import { connect, disconnect } from "../../helpers/db";
import type mongoose from "mongoose";
import { Advertisement } from "../../../src/models";
import { beforeAll, afterAll, describe, it, expect } from "vitest";
import { createTestPlugin } from "../../helpers/plugins";
import { getAdvertisements } from "../../../src/resolvers/Query/getAdvertisements";

let MONGOOSE_INSTANCE: typeof mongoose;

beforeAll(async () => {
MONGOOSE_INSTANCE = await connect();
await createTestPlugin();
});

afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

describe("resolvers -> Query -> getAdvertisment", () => {
it(`returns list of all existing advertisement`, async () => {
const adsPayload = await getAdvertisements?.({}, {}, {});

const ads = await Advertisement.find().lean();

expect(adsPayload).toEqual(ads);
});
});

0 comments on commit e0db32f

Please sign in to comment.