Skip to content

Commit a6a3f33

Browse files
authored
Simulated Testing Database For Talawa Admin (PalisadoesFoundation#1369)
* Fixed Merge Conflicts * Removed Unnecessary Test Assertions * Increased Test Coverage * fixed package.json conflict * removed extra whitespace * updated package.json * added package.json * re-aded faker-js/faker package * removed unnecessary code
1 parent 5565c3e commit a6a3f33

27 files changed

+1230
-0
lines changed

locales/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"organization.profileImage.notFound": "Organization profile image not found",
1313
"organization.member.notFound": "Organization's user is not a member",
1414
"organization.notAuthorized": "Organization is not authorized",
15+
"sampleOrganization.duplicate": "Sample Organization already generated",
1516
"membershipRequest.notFound": "Membership Request not found",
1617
"membershipRequest.alreadyExists": "Membership Request already exists",
1718
"chat.notFound": "Chat not found",

locales/fr.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"organization.profileImage.notFound": "Image du profil de l'organisation introuvable",
1212
"organization.member.notFound": "L'utilisateur de l'organisation n'est pas membre",
1313
"organization.notAuthorized": "L'organisation n'est pas autorisée",
14+
"sampleOrganization.duplicate": "Exemple d'organisation déjà généré",
1415
"membershipRequest.notFound": "Demande d'adhésion introuvable",
1516
"membershipRequest.alreadyExists": "La demande d'adhésion existe déjà",
1617
"chat.notFound": "Chat non trouvé",

locales/hi.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"organization.profileImage.notFound": "संगठन की प्रोफ़ाइल छवि नहीं मिली",
1313
"organization.member.notFound": "संगठन का उपयोगकर्ता सदस्य नहीं है",
1414
"organization.notAuthorized": "संगठन अधिकृत नहीं है",
15+
"sampleOrganization.dulicate": "नमूना संगठन पहले ही तैयार हो चुका है",
1516
"membershipRequest.notFound": "सदस्यता अनुरोध नहीं मिला",
1617
"membershipRequest.alreadyExists": "सदस्यता अनुरोध पहले से मौजूद है",
1718
"chat.notFound": "चैट नहीं मिली",

locales/sp.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"organization.profileImage.notFound": "No se encontró la imagen del perfil de la organización",
1212
"organization.member.notFound": "El usuario de la organización no es miembro",
1313
"organization.notAuthorized": "La organización no está autorizada",
14+
"sampleOrganization.duplicate": "Organización de muestra ya generada",
1415
"membershipRequest.notFound": "Solicitud de membresía no encontrada",
1516
"membershipRequest.alreadyExists": "La solicitud de membresía ya existe",
1617
"chat.notFound": "Chat no encontrado",

locales/zh.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"organization.profileImage.notFound": "未找到組織檔案圖像",
1212
"organization.member.notFound": "組織的用戶不是成員",
1313
"organization.notAuthorized": "組織未授權",
14+
"sampleOrganization.duplicate": "已生成样本组织",
1415
"membershipRequest.notFound": "未找到會員請求",
1516
"membershipRequest.alreadyExists": "成員資格請求已存在",
1617
"chat.notFound": "未找到聊天",

package-lock.json

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"homepage": "https://github.com/PalisadoesFoundation/talawa-api#readme",
4343
"dependencies": {
4444
"@apollo/server": "^4.9.3",
45+
"@faker-js/faker": "^8.2.0",
4546
"@graphql-inspector/cli": "^3.4.19",
4647
"@graphql-tools/resolvers-composition": "^7.0.0",
4748
"@graphql-tools/schema": "^10.0.0",

schema.graphql

+3
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ type Mutation {
444444
createOrganization(data: OrganizationInput, file: String): Organization!
445445
createPlugin(pluginCreatedBy: String!, pluginDesc: String!, pluginName: String!, uninstalledOrgs: [ID!]): Plugin!
446446
createPost(data: PostInput!, file: String): Post
447+
createSampleOrganization: Boolean!
447448
createTask(data: TaskInput!, eventProjectId: ID!): Task!
448449
createUserTag(input: CreateUserTagInput!): UserTag
449450
deleteAdvertisementById(id: ID!): DeletePayload!
@@ -473,6 +474,7 @@ type Mutation {
473474
removeOrganization(id: ID!): User!
474475
removeOrganizationImage(organizationId: String!): Organization!
475476
removePost(id: ID!): Post
477+
removeSampleOrganization: Boolean!
476478
removeTask(id: ID!): Task
477479
removeUserFromGroupChat(chatId: ID!, userId: ID!): GroupChat!
478480
removeUserImage: User!
@@ -743,6 +745,7 @@ type Query {
743745
getPlugins: [Plugin]
744746
getlanguage(lang_code: String!): [Translation]
745747
hasSubmittedFeedback(eventId: ID!, userId: ID!): Boolean
748+
isSampleOrganization(id: ID!): Boolean!
746749
joinedOrganizations(id: ID): [Organization]
747750
me: User!
748751
myLanguage: String

src/constants.ts

+7
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,13 @@ export const USER_ALREADY_CHECKED_IN = {
393393
PARAM: "user.alreadyCheckedIn",
394394
};
395395

396+
export const SAMPLE_ORGANIZATION_ALREADY_EXISTS = {
397+
DESC: "Sample Organization was already generated",
398+
CODE: "sampleOrganization.duplicate",
399+
MESSAGE: "sampleOrganization.duplicate",
400+
PARAM: "sampleOrganization",
401+
};
402+
396403
export const MAXIMUM_FETCH_LIMIT = 100;
397404

398405
export const BASE_URL = `http://localhost:${process.env.port || 4000}/`;

src/models/SampleData.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Model } from "mongoose";
2+
import { Schema, model, models } from "mongoose";
3+
4+
export interface InterfaceSampleData {
5+
documentId: string;
6+
collectionName: "Organization" | "Post" | "Event" | "User" | "Plugin";
7+
}
8+
9+
const sampleDataSchema = new Schema<InterfaceSampleData>({
10+
documentId: {
11+
type: String,
12+
required: true,
13+
},
14+
collectionName: {
15+
type: String,
16+
required: true,
17+
enum: ["Organization", "Post", "Event", "User", "Plugin"],
18+
},
19+
});
20+
21+
const sampleDataModel = (): Model<InterfaceSampleData> =>
22+
model<InterfaceSampleData>("SampleData", sampleDataSchema);
23+
24+
export const SampleData = (models.SampleData ||
25+
sampleDataModel()) as ReturnType<typeof sampleDataModel>;

src/models/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export * from "./OrganizationTagUser";
2222
export * from "./Plugin";
2323
export * from "./PluginField";
2424
export * from "./Post";
25+
export * from "./SampleData";
2526
export * from "./Task";
2627
export * from "./TaskVolunteer";
2728
export * from "./User";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
USER_NOT_AUTHORIZED_ERROR,
3+
USER_NOT_FOUND_ERROR,
4+
SAMPLE_ORGANIZATION_ALREADY_EXISTS,
5+
} from "../../constants";
6+
import { errors, requestContext } from "../../libraries";
7+
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
8+
import { createSampleOrganization as createSampleOrgUtil } from "../../utilities/createSampleOrganizationUtil";
9+
import { SampleData, User } from "../../models";
10+
11+
/**
12+
* Generates sample data for testing or development purposes.
13+
* @returns True if the sample data generation is successful, false otherwise.
14+
*/
15+
export const createSampleOrganization: MutationResolvers["createSampleOrganization"] =
16+
async (_parent, _args, _context) => {
17+
const currentUser = await User.findOne({
18+
_id: _context.userId,
19+
}).lean();
20+
21+
if (!currentUser) {
22+
throw new errors.NotFoundError(
23+
requestContext.translate(USER_NOT_FOUND_ERROR.MESSAGE),
24+
USER_NOT_FOUND_ERROR.CODE,
25+
USER_NOT_FOUND_ERROR.PARAM
26+
);
27+
}
28+
29+
if (
30+
!(
31+
currentUser.userType === "SUPERADMIN" ||
32+
currentUser.userType === "ADMIN"
33+
)
34+
) {
35+
throw new errors.UnauthorizedError(
36+
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
37+
USER_NOT_AUTHORIZED_ERROR.CODE,
38+
USER_NOT_AUTHORIZED_ERROR.PARAM
39+
);
40+
}
41+
42+
const existingOrganization = await SampleData.findOne({
43+
collectionName: "Organization",
44+
});
45+
46+
if (existingOrganization) {
47+
throw new errors.UnauthorizedError(
48+
requestContext.translate(SAMPLE_ORGANIZATION_ALREADY_EXISTS.MESSAGE),
49+
SAMPLE_ORGANIZATION_ALREADY_EXISTS.CODE,
50+
SAMPLE_ORGANIZATION_ALREADY_EXISTS.PARAM
51+
);
52+
}
53+
54+
await createSampleOrgUtil();
55+
return true;
56+
};

src/resolvers/Mutation/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { createOrganization } from "./createOrganization";
2828
import { createPlugin } from "./createPlugin";
2929
import { createAdvertisement } from "./createAdvertisement";
3030
import { createPost } from "./createPost";
31+
import { createSampleOrganization } from "./createSampleOrganization";
3132
import { createTask } from "./createTask";
3233
import { createUserTag } from "./createUserTag";
3334
import { deleteDonationById } from "./deleteDonationById";
@@ -56,6 +57,7 @@ import { removeMember } from "./removeMember";
5657
import { removeOrganization } from "./removeOrganization";
5758
import { removeOrganizationImage } from "./removeOrganizationImage";
5859
import { removePost } from "./removePost";
60+
import { removeSampleOrganization } from "./removeSampleOrganization";
5961
import { removeTask } from "./removeTask";
6062
import { removeUserFromGroupChat } from "./removeUserFromGroupChat";
6163
import { removeUserImage } from "./removeUserImage";
@@ -116,6 +118,7 @@ export const Mutation: MutationResolvers = {
116118
createOrganization,
117119
createPlugin,
118120
createPost,
121+
createSampleOrganization,
119122
createTask,
120123
createUserTag,
121124
deleteDonationById,
@@ -144,6 +147,7 @@ export const Mutation: MutationResolvers = {
144147
removeMember,
145148
removeOrganization,
146149
removeOrganizationImage,
150+
removeSampleOrganization,
147151
removePost,
148152
removeTask,
149153
removeUserFromGroupChat,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
2+
import { removeSampleOrganization as removeSampleOrgUtil } from "../../utilities/removeSampleOrganizationUtil";
3+
import { SampleData, User } from "../../models";
4+
import { errors, requestContext } from "../../libraries";
5+
import {
6+
ORGANIZATION_NOT_FOUND_ERROR,
7+
USER_NOT_AUTHORIZED_ERROR,
8+
USER_NOT_FOUND_ERROR,
9+
} from "../../constants";
10+
11+
export const removeSampleOrganization: MutationResolvers["removeSampleOrganization"] =
12+
async (_parent, _args, _context) => {
13+
const currentUser = await User.findOne({
14+
_id: _context.userId,
15+
}).lean();
16+
17+
if (!currentUser) {
18+
throw new errors.NotFoundError(
19+
requestContext.translate(USER_NOT_FOUND_ERROR.MESSAGE),
20+
USER_NOT_FOUND_ERROR.CODE,
21+
USER_NOT_FOUND_ERROR.PARAM
22+
);
23+
}
24+
25+
if (
26+
!(
27+
currentUser.userType === "SUPERADMIN" ||
28+
currentUser.userType === "ADMIN"
29+
)
30+
) {
31+
throw new errors.UnauthorizedError(
32+
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
33+
USER_NOT_AUTHORIZED_ERROR.CODE,
34+
USER_NOT_AUTHORIZED_ERROR.PARAM
35+
);
36+
}
37+
38+
const existingOrganization = await SampleData.findOne({
39+
collectionName: "Organization",
40+
});
41+
42+
if (!existingOrganization) {
43+
throw new errors.NotFoundError(
44+
requestContext.translate(ORGANIZATION_NOT_FOUND_ERROR.MESSAGE),
45+
ORGANIZATION_NOT_FOUND_ERROR.CODE,
46+
ORGANIZATION_NOT_FOUND_ERROR.PARAM
47+
);
48+
}
49+
50+
await removeSampleOrgUtil();
51+
return true;
52+
};

src/resolvers/Query/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { myLanguage } from "./myLanguage";
1515
import { organizations } from "./organizations";
1616
import { organizationsConnection } from "./organizationsConnection";
1717
import { organizationsMemberConnection } from "./organizationsMemberConnection";
18+
import { isSampleOrganization } from "../Query/organizationIsSample";
1819
import { post } from "./post";
1920
import { postsByOrganization } from "./postsByOrganization";
2021
import { postsByOrganizationConnection } from "./postsByOrganizationConnection";
@@ -38,6 +39,7 @@ export const Query: QueryResolvers = {
3839
getDonationByOrgIdConnection,
3940
getlanguage,
4041
getPlugins,
42+
isSampleOrganization,
4143
me,
4244
myLanguage,
4345
organizations,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { QueryResolvers } from "../../types/generatedGraphQLTypes";
2+
import { Organization, SampleData } from "../../models";
3+
import { errors, requestContext } from "../../libraries";
4+
import { ORGANIZATION_NOT_FOUND_ERROR } from "../../constants";
5+
6+
export const isSampleOrganization: QueryResolvers["isSampleOrganization"] =
7+
async (_parent, args) => {
8+
const organizationId = args.id;
9+
10+
const organization = await Organization.findById(args.id);
11+
12+
if (!organization) {
13+
throw new errors.UnauthorizedError(
14+
requestContext.translate(ORGANIZATION_NOT_FOUND_ERROR.MESSAGE),
15+
ORGANIZATION_NOT_FOUND_ERROR.CODE,
16+
ORGANIZATION_NOT_FOUND_ERROR.PARAM
17+
);
18+
}
19+
20+
const sampleOrganization = await SampleData.findOne({
21+
documentId: organizationId,
22+
});
23+
24+
return !!sampleOrganization;
25+
};

src/resolvers/Subscription/onPluginUpdate.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { withFilter } from "graphql-subscriptions";
22
import type { SubscriptionResolvers } from "../../types/generatedGraphQLTypes";
33

4+
// import { GroupChat } from "../GroupChat";
5+
46
const TALAWA_PLUGIN_UPDATED = "TALAWA_PLUGIN_UPDATED";
57
/**
68
* This property included a `subscribe` method, which is used to

src/typeDefs/mutations.ts

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export const mutations = gql`
8989
9090
createUserTag(input: CreateUserTagInput!): UserTag @auth
9191
92+
createSampleOrganization: Boolean! @auth
93+
9294
createTask(data: TaskInput!, eventProjectId: ID!): Task! @auth
9395
9496
deleteAdvertisementById(id: ID!): DeletePayload!
@@ -149,6 +151,8 @@ export const mutations = gql`
149151
150152
removeUserTag(id: ID!): UserTag @auth
151153
154+
removeSampleOrganization: Boolean! @auth
155+
152156
removeTask(id: ID!): Task @auth
153157
154158
removeUserFromGroupChat(userId: ID!, chatId: ID!): GroupChat! @auth

src/typeDefs/queries.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const queries = gql`
4040
getPlugins: [Plugin]
4141
getAdvertisements: [Advertisement]
4242
43+
isSampleOrganization(id: ID!): Boolean!
4344
hasSubmittedFeedback(userId: ID!, eventId: ID!): Boolean
4445
4546
joinedOrganizations(id: ID): [Organization]

0 commit comments

Comments
 (0)