@@ -33,6 +33,11 @@ const {
3333 groupUpdateLastJoinDate,
3434 updateIdleUsersOnDiscord,
3535 updateIdle7dUsersOnDiscord,
36+ updateUsersWith31DaysPlusOnboarding,
37+ updateGroupRole,
38+ removeMemberGroup,
39+ getGroupRoleByName,
40+ getGroupRolesForUser,
3641} = require ( "../../../models/discordactions" ) ;
3742const {
3843 groupData,
@@ -41,6 +46,7 @@ const {
4146 memberGroupData,
4247 groupIdle,
4348 groupIdle7d,
49+ groupOnboarding31dPlus,
4450} = require ( "../../fixtures/discordactions/discordactions" ) ;
4551const cleanDb = require ( "../../utils/cleanDb" ) ;
4652const { userPhotoVerificationData } = require ( "../../fixtures/user/photo-verification" ) ;
@@ -54,6 +60,7 @@ const { createProgressDocument } = require("../../../models/progresses");
5460const { stubbedModelTaskProgressData } = require ( "../../fixtures/progress/progresses" ) ;
5561const { convertDaysToMilliseconds } = require ( "../../../utils/time" ) ;
5662const { generateUserStatusData } = require ( "../../fixtures/userStatus/userStatus" ) ;
63+ const { userState } = require ( "../../../constants/userStatus" ) ;
5764
5865chai . should ( ) ;
5966
@@ -993,4 +1000,243 @@ describe("discordactions", function () {
9931000 expect ( res . totalArchivedUsers ) . to . be . equal ( 1 ) ;
9941001 } ) ;
9951002 } ) ;
1003+
1004+ describe ( "updateUsersWith31DaysPlusOnboarding" , function ( ) {
1005+ let fetchStub ;
1006+
1007+ beforeEach ( async function ( ) {
1008+ fetchStub = sinon . stub ( global , "fetch" ) ;
1009+
1010+ userData [ 0 ] = {
1011+ ...userData [ 0 ] ,
1012+ discordId : "2131234453456545656765767876" ,
1013+ discordJoinedAt : "2023-07-31T16:57:53.894000+00:00" ,
1014+ roles : { archived : false , in_discord : true } ,
1015+ } ;
1016+ userData [ 1 ] = {
1017+ ...userData [ 1 ] ,
1018+ discordId : "12345678909867666" ,
1019+ discordJoinedAt : "2023-07-31T16:57:53.894000+00:00" ,
1020+ roles : { archived : false , in_discord : true } ,
1021+ } ;
1022+ userData [ 2 ] = {
1023+ ...userData [ 2 ] ,
1024+ discordId : "1234567" ,
1025+ discordJoinedAt : "2023-07-31T16:57:53.894000+00:00" ,
1026+ roles : { archived : false , in_discord : true } ,
1027+ } ;
1028+
1029+ const userId0 = await addUser ( userData [ 0 ] ) ;
1030+ const userId1 = await addUser ( userData [ 1 ] ) ;
1031+ const userId2 = await addUser ( userData [ 2 ] ) ;
1032+
1033+ await userStatusModel . updateUserStatus (
1034+ userId0 ,
1035+ generateUserStatusData ( userState . ONBOARDING , 1690829925336 , 1690829925336 )
1036+ ) ;
1037+ await userStatusModel . updateUserStatus (
1038+ userId1 ,
1039+ generateUserStatusData ( userState . ONBOARDING , 1690829925336 , 1690829925336 )
1040+ ) ;
1041+ await userStatusModel . updateUserStatus (
1042+ userId2 ,
1043+ generateUserStatusData ( userState . IDLE , 1690829925336 , 1690829925336 )
1044+ ) ;
1045+
1046+ const addRolesPromises = [ discordRoleModel . add ( groupOnboarding31dPlus ) ] ;
1047+ await Promise . all ( addRolesPromises ) ;
1048+
1049+ getDiscordMembers [ 1 ] = {
1050+ ...getDiscordMembers [ 1 ] ,
1051+ roles : [ "9876543210" ] ,
1052+ } ;
1053+ getDiscordMembers [ 3 ] = {
1054+ ...getDiscordMembers [ 3 ] ,
1055+ roles : [ "9876543210" , "11334336" ] ,
1056+ } ;
1057+ getDiscordMembers [ 4 ] = {
1058+ ...getDiscordMembers [ 4 ] ,
1059+ roles : [ "9876543210" , "11334336" ] ,
1060+ } ;
1061+
1062+ fetchStub . returns (
1063+ Promise . resolve ( {
1064+ status : 200 ,
1065+ json : ( ) => Promise . resolve ( getDiscordMembers ) ,
1066+ } )
1067+ ) ;
1068+ } ) ;
1069+
1070+ afterEach ( async function ( ) {
1071+ sinon . restore ( ) ;
1072+ await cleanDb ( ) ;
1073+ } ) ;
1074+
1075+ it ( "apply, or remove grouponboarding31D" , async function ( ) {
1076+ const res = await updateUsersWith31DaysPlusOnboarding ( ) ;
1077+
1078+ expect ( res . usersAlreadyHavingOnboaring31DaysRole . count ) . to . be . equal ( 2 ) ;
1079+ expect ( res . totalOnboardingUsers31DaysCompleted . count ) . to . be . equal ( 2 ) ;
1080+ expect ( res . totalOnboarding31dPlusRoleApplied . count ) . to . be . equal ( 1 ) ;
1081+ expect ( res . totalOnboarding31dPlusRoleRemoved . count ) . to . be . equal ( 1 ) ;
1082+ } ) ;
1083+
1084+ it ( "should throw an error if Database error occurs" , async function ( ) {
1085+ fetchStub . rejects ( new Error ( "Database error" ) ) ;
1086+
1087+ try {
1088+ await updateUsersWith31DaysPlusOnboarding ( ) ;
1089+ } catch ( err ) {
1090+ expect ( err ) . to . be . an . instanceOf ( Error ) ;
1091+ expect ( err . message ) . to . equal ( "Error while fetching onboarding users who have completed 31 days" ) ;
1092+ }
1093+ } ) ;
1094+ } ) ;
1095+
1096+ describe ( "removeMemberGroup" , function ( ) {
1097+ let deleteStub ;
1098+
1099+ beforeEach ( async function ( ) {
1100+ await discordRoleModel . add ( { roleid : "1234566777777" , rolename : "xyz" } ) ;
1101+ await memberRoleModel . add ( { roleid : "1234566777777" , userid : "2131234453456545656765767876" } ) ;
1102+
1103+ deleteStub = sinon . stub ( ) ;
1104+ sinon . stub ( memberRoleModel , "doc" ) . returns ( {
1105+ delete : deleteStub . resolves ( ) ,
1106+ } ) ;
1107+ } ) ;
1108+
1109+ afterEach ( async function ( ) {
1110+ sinon . restore ( ) ;
1111+ await cleanDb ( ) ;
1112+ } ) ;
1113+
1114+ it ( "should remove role from user" , async function ( ) {
1115+ const res = await removeMemberGroup ( "1234566777777" , "2131234453456545656765767876" ) ;
1116+
1117+ expect ( res . roleId ) . to . be . equal ( "1234566777777" ) ;
1118+ expect ( res . wasSuccess ) . to . be . equal ( true ) ;
1119+ } ) ;
1120+
1121+ it ( "should return wasSuccess as false if role does not exist" , async function ( ) {
1122+ const res = await removeMemberGroup ( "123" , "2131234453456545656765767876" ) ;
1123+
1124+ expect ( res . roleId ) . to . be . equal ( "123" ) ;
1125+ expect ( res . wasSuccess ) . to . be . equal ( false ) ;
1126+ } ) ;
1127+
1128+ it ( "return error if something goes while deleting the role" , async function ( ) {
1129+ deleteStub . rejects ( new Error ( "Database error" ) ) ;
1130+
1131+ try {
1132+ await removeMemberGroup ( "1243" , "2131234453456545656765767876" ) ;
1133+ } catch ( err ) {
1134+ expect ( err ) . to . be . an . instanceOf ( Error ) ;
1135+ expect ( err . message ) . to . equal ( "Database error" ) ;
1136+ }
1137+ } ) ;
1138+ } ) ;
1139+
1140+ describe ( "getGroupRolesForUser" , function ( ) {
1141+ let whereStub ;
1142+
1143+ beforeEach ( async function ( ) {
1144+ whereStub = sinon . stub ( memberRoleModel , "where" ) . resolves ( ) ;
1145+
1146+ await discordRoleModel . add ( { roleid : "1234566777777" , rolename : "xyz" } ) ;
1147+ await memberRoleModel . add ( { roleid : "1234566777777" , userid : "2131234453456545656765767876" } ) ;
1148+ } ) ;
1149+
1150+ afterEach ( async function ( ) {
1151+ sinon . restore ( ) ;
1152+ await cleanDb ( ) ;
1153+ } ) ;
1154+
1155+ it ( "should return group role for a given user" , async function ( ) {
1156+ const res = await getGroupRolesForUser ( "2131234453456545656765767876" ) ;
1157+
1158+ expect ( res . userId ) . to . be . equal ( "2131234453456545656765767876" ) ;
1159+ expect ( res . groups [ 0 ] . roleId ) . to . be . equal ( "1234566777777" ) ;
1160+ } ) ;
1161+
1162+ it ( "should throw error if something goes wrong while fetching the roles" , async function ( ) {
1163+ whereStub . rejects ( new Error ( "Something went wrong while fetching" ) ) ;
1164+
1165+ try {
1166+ await getGroupRolesForUser ( "2131234453456545656765767876" ) ;
1167+ } catch ( err ) {
1168+ expect ( err ) . to . be . an . instanceOf ( Error ) ;
1169+ expect ( err . message ) . to . equal ( "Something went wrong while fetching" ) ;
1170+ }
1171+ } ) ;
1172+ } ) ;
1173+
1174+ describe ( "getGroupRoleByName" , function ( ) {
1175+ let whereStub ;
1176+
1177+ beforeEach ( async function ( ) {
1178+ await discordRoleModel . add ( { roleid : "1234566777777" , rolename : "xyz" } ) ;
1179+ whereStub = sinon . stub ( discordRoleModel , "where" ) . resolves ( ) ;
1180+ } ) ;
1181+
1182+ afterEach ( async function ( ) {
1183+ sinon . restore ( ) ;
1184+ await cleanDb ( ) ;
1185+ } ) ;
1186+
1187+ it ( "should return the role, if given the name" , async function ( ) {
1188+ const res = await getGroupRoleByName ( "xyz" ) ;
1189+
1190+ expect ( res . data ) . to . be . an . instanceOf ( Object ) ;
1191+ } ) ;
1192+
1193+ it ( "should thrown an error, if something goes wrong" , async function ( ) {
1194+ whereStub . rejects ( new Error ( "Database Error" ) ) ;
1195+
1196+ try {
1197+ await getGroupRoleByName ( "xyz" ) ;
1198+ } catch ( err ) {
1199+ expect ( err ) . to . be . an . instanceOf ( Error ) ;
1200+ expect ( err . message ) . to . equal ( "Database error" ) ;
1201+ }
1202+ } ) ;
1203+ } ) ;
1204+
1205+ describe ( "updateGroupRole" , function ( ) {
1206+ let setStub ;
1207+ let addStub ;
1208+
1209+ beforeEach ( async function ( ) {
1210+ addStub = sinon . stub ( discordRoleModel , "add" ) ;
1211+
1212+ addStub . resolves ( { id : "test-id" } ) ;
1213+
1214+ setStub = sinon . stub ( ) ;
1215+ sinon . stub ( discordRoleModel , "where" ) . returns ( {
1216+ set : setStub . resolves ( ) ,
1217+ } ) ;
1218+ } ) ;
1219+
1220+ afterEach ( async function ( ) {
1221+ sinon . restore ( ) ;
1222+ await cleanDb ( ) ;
1223+ } ) ;
1224+
1225+ it ( "should update the group role data" , async function ( ) {
1226+ const res = await updateGroupRole ( { roleid : "2312" , rolename : "fmk" } , "test-id" ) ;
1227+
1228+ expect ( res . data ) . to . be . an . instanceOf ( Object ) ;
1229+ } ) ;
1230+
1231+ it ( "should throw an error if something goes wrong" , async function ( ) {
1232+ setStub . rejects ( new Error ( "Database error" ) ) ;
1233+
1234+ try {
1235+ await updateGroupRole ( { roleid : "2312" , rolename : "fmk" } , "test-id" ) ;
1236+ } catch ( err ) {
1237+ expect ( err ) . to . be . an . instanceOf ( Error ) ;
1238+ expect ( err . message ) . to . equal ( "Database error" ) ;
1239+ }
1240+ } ) ;
1241+ } ) ;
9961242} ) ;
0 commit comments