11import { Promise } from 'meteor/promise' ;
22import { Meteor } from 'meteor/meteor' ;
33import { Match , check } from 'meteor/check' ;
4+ import { TextObjectType , BlockType } from '@rocket.chat/apps-engine/definition/uikit' ;
45
56import { API } from '../api' ;
67import { Banner } from '../../../../server/sdk' ;
7- import { BannerPlatform } from '../../../../definition/IBanner' ;
8+ import { BannerPlatform , IBanner } from '../../../../definition/IBanner' ;
89
9- API . v1 . addRoute ( 'banners.getNew' , { authRequired : true } , {
10+ API . v1 . addRoute ( 'banners.getNew' , { authRequired : true } , { // deprecated
1011 get ( ) {
1112 check ( this . queryParams , Match . ObjectIncluding ( {
1213 platform : String ,
@@ -22,12 +23,129 @@ API.v1.addRoute('banners.getNew', { authRequired: true }, {
2223 throw new Meteor . Error ( 'error-unknown-platform' , 'Platform is unknown.' ) ;
2324 }
2425
25- const banners = Promise . await ( Banner . getNewBannersForUser ( this . userId , platform , bannerId ) ) ;
26+ const banners = Promise . await ( Banner . getBannersForUser ( this . userId , platform , bannerId ) ) ;
2627
2728 return API . v1 . success ( { banners } ) ;
2829 } ,
2930} ) ;
3031
32+
33+ API . v1 . addRoute ( 'banners/:id' , { authRequired : true } , {
34+
35+ get ( ) {
36+ check ( this . urlParams , Match . ObjectIncluding ( {
37+ id : String ,
38+ } ) ) ;
39+
40+ const { platform } = this . queryParams ;
41+ if ( ! platform ) {
42+ throw new Meteor . Error ( 'error-missing-param' , 'The required "platform" param is missing.' ) ;
43+ }
44+
45+ const { id } = this . urlParams ;
46+ if ( ! id ) {
47+ throw new Meteor . Error ( 'error-missing-param' , 'The required "id" param is missing.' ) ;
48+ }
49+
50+ const banners = Promise . await ( Banner . getBannersForUser ( this . userId , platform , id ) ) ;
51+
52+ return API . v1 . success ( { banners } ) ;
53+ } ,
54+ } ) ;
55+ API . v1 . addRoute ( 'banners' , { authRequired : true } , {
56+
57+ get ( ) {
58+ check ( this . queryParams , Match . ObjectIncluding ( {
59+ platform : String ,
60+ } ) ) ;
61+
62+ const { platform } = this . queryParams ;
63+ if ( ! platform ) {
64+ throw new Meteor . Error ( 'error-missing-param' , 'The required "platform" param is missing.' ) ;
65+ }
66+
67+ if ( ! Object . values ( BannerPlatform ) . includes ( platform ) ) {
68+ throw new Meteor . Error ( 'error-unknown-platform' , 'Platform is unknown.' ) ;
69+ }
70+
71+ const banners = Promise . await ( Banner . getBannersForUser ( this . userId , platform ) ) ;
72+
73+ return API . v1 . success ( { banners } ) ;
74+ } ,
75+ ...process . env . NODE_ENV !== 'production' && {
76+ post ( ) : { } {
77+ check ( this . bodyParams , Match . ObjectIncluding ( {
78+ platform : Match . Maybe ( String ) ,
79+ bid : String ,
80+ } ) ) ;
81+
82+ const { platform = 'web' , bid : bannerId } = this . bodyParams ;
83+
84+ if ( ! platform ) {
85+ throw new Meteor . Error ( 'error-missing-param' , 'The required "platform" param is missing.' ) ;
86+ }
87+
88+ if ( ! Object . values ( BannerPlatform ) . includes ( platform ) ) {
89+ throw new Meteor . Error ( 'error-unknown-platform' , 'Platform is unknown.' ) ;
90+ }
91+ const b : IBanner = {
92+ _id : bannerId ,
93+ platform : [ platform ] ,
94+ expireAt : new Date ( new Date ( ) . getTime ( ) + ( 1000 * 60 * 60 * 24 * 7 ) ) ,
95+ startAt : new Date ( ) ,
96+ roles : [ 'admin' ] ,
97+ createdBy : {
98+ _id : this . userId ,
99+ username : this . userId ,
100+ } ,
101+ createdAt : new Date ( ) ,
102+ _updatedAt : new Date ( ) ,
103+ view : {
104+ viewId : '' ,
105+ appId : '' ,
106+ blocks : [ {
107+ type : BlockType . SECTION ,
108+ blockId : 'attention' ,
109+ text : {
110+ type : TextObjectType . PLAINTEXT ,
111+ text : 'Test' ,
112+ emoji : false ,
113+ } ,
114+ } ] ,
115+ } ,
116+ } ;
117+
118+ const banners = Promise . await ( Banner . create ( b ) ) ;
119+
120+ return API . v1 . success ( { banners } ) ;
121+ } ,
122+ delete ( ) : { } {
123+ check ( this . bodyParams , Match . ObjectIncluding ( {
124+ bid : String ,
125+ } ) ) ;
126+
127+ const { bid } = this . bodyParams ;
128+
129+ Promise . await ( Banner . disable ( bid ) ) ;
130+
131+ return API . v1 . success ( ) ;
132+ } ,
133+
134+ patch ( ) : { } {
135+ check ( this . bodyParams , Match . ObjectIncluding ( {
136+ bid : String ,
137+ } ) ) ;
138+
139+ const { bid } = this . bodyParams ;
140+
141+ Promise . await ( Banner . enable ( bid ) ) ;
142+
143+ return API . v1 . success ( ) ;
144+ } ,
145+ } ,
146+ } ) ;
147+
148+
31149API . v1 . addRoute ( 'banners.dismiss' , { authRequired : true } , {
32150 post ( ) {
33151 check ( this . bodyParams , Match . ObjectIncluding ( {
0 commit comments