forked from PalisadoesFoundation/talawa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Advertisement feature in API (PalisadoesFoundation#1395)
* 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
- Loading branch information
1 parent
07248d6
commit b24c407
Showing
22 changed files
with
715 additions
and
119 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -91,3 +91,4 @@ LOG_LEVEL = info | |
|
||
REDIS_HOST= | ||
REDIS_PORT= | ||
REDIS_PASSWORD= |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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,81 @@ | ||
import type { Types, Model } from "mongoose"; | ||
import { Schema, model, models } from "mongoose"; | ||
/** | ||
* This is an interface that represents a database(MongoDB) document for Advertisement. | ||
*/ | ||
type AdvertisementTypes = { | ||
type: "POPUP" | "MENU" | "BANNER"; | ||
// Other properties specific to each type | ||
}; | ||
export interface InterfaceAdvertisement { | ||
_id: Types.ObjectId; | ||
orgId: string; | ||
name: string; | ||
link: string; | ||
type: AdvertisementTypes; | ||
startDate: string; | ||
endDate: string; | ||
} | ||
|
||
/** | ||
* @param name - Name of the advertisement (type: String) | ||
* Description: Name of the advertisement. | ||
*/ | ||
|
||
/** | ||
* @param orgId - Organization ID associated with the advertisement (type: Schema.Types.ObjectId) | ||
* Description: Organization ID associated with the advertisement. | ||
*/ | ||
|
||
/** | ||
* @param link - Link associated with the advertisement (type: String) | ||
* Description: Link associated with the advertisement. | ||
*/ | ||
|
||
/** | ||
* @param type - Type of advertisement (POPUP, MENU, BANNER) (type: String) | ||
* Description: Type of advertisement (POPUP, MENU, BANNER). | ||
*/ | ||
|
||
/** | ||
* @param startDate - Start date of the advertisement (type: Date) | ||
* Description: Start date of the advertisement. | ||
*/ | ||
|
||
/** | ||
* @param endDate - End date of the advertisement (type: Date) | ||
* Description: End date of the advertisement. | ||
*/ | ||
const advertisementSchema = new Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
orgId: { | ||
type: String, | ||
}, | ||
link: { | ||
type: String, | ||
required: true, | ||
}, | ||
type: { | ||
type: String, | ||
enum: ["POPUP", "MENU", "BANNER"], | ||
required: true, | ||
}, | ||
startDate: { | ||
type: String, | ||
required: true, | ||
}, | ||
endDate: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
|
||
const advertisementModel = (): Model<InterfaceAdvertisement> => | ||
model<InterfaceAdvertisement>("Advertisement", advertisementSchema); | ||
|
||
// This syntax is needed to prevent Mongoose OverwriteModelError while running tests. | ||
export const Advertisement = (models.Advertisement || | ||
advertisementModel()) as ReturnType<typeof advertisementModel>; |
This file contains 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 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,18 @@ | ||
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, | ||
}); | ||
// Returns createdAd. | ||
return createdAd.toObject(); | ||
}; |
This file contains 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 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,28 @@ | ||
import type { MutationResolvers } from "../../types/generatedGraphQLTypes"; | ||
import { errors, requestContext } from "../../libraries"; | ||
import { Advertisement } from "../../models"; | ||
import { ADVERTISEMENT_NOT_FOUND_ERROR } from "../../constants"; | ||
|
||
// @ts-ignore | ||
export const removeAdvertisement: MutationResolvers["removeAdvertisement"] = | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async (_parent, args, _context) => { | ||
const currentAd = await Advertisement.findOne({ | ||
_id: args.id ? args.id : "", | ||
}).lean(); | ||
|
||
if (!currentAd) { | ||
throw new errors.NotFoundError( | ||
requestContext.translate(ADVERTISEMENT_NOT_FOUND_ERROR.MESSAGE), | ||
ADVERTISEMENT_NOT_FOUND_ERROR.CODE, | ||
ADVERTISEMENT_NOT_FOUND_ERROR.PARAM | ||
); | ||
} | ||
|
||
// Deletes the ad. | ||
await Advertisement.deleteOne({ | ||
_id: args.id ? args.id : "", | ||
}); | ||
// Returns deleted ad. | ||
return currentAd; | ||
}; |
This file contains 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,11 @@ | ||
import type { QueryResolvers } from "../../types/generatedGraphQLTypes"; | ||
import { Advertisement } from "../../models"; | ||
|
||
/** | ||
* This function returns list of Advertisement from the database. | ||
* @returns An object that contains a list of Ads. | ||
*/ | ||
export const getAdvertisements: QueryResolvers["getAdvertisements"] = | ||
async () => { | ||
return await Advertisement.find().lean(); | ||
}; |
This file contains 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 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 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 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 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 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
Oops, something went wrong.