The endoscopy is to accertain if there are any irregularities inside your stomach such as an ulcer or a bacterial infection that some people have without knowing. If found the bacterial infection can be treated and the same goes for an ulcer. Once operated, especially Gastric Bypass, there is no way of treating the symptoms orally due to the stomach being detached from the main stomach, and would lead to more major surgery through the abdomen wall say if you suddenly got a perforated ulcer. Before bypass surgery, treatments for either of these conditions is relatively straight forward and accessable to treat orally.
If you are found to have these problems when you have your endoscopy, you would have treatment to clear you of the problems before your surgery was done. You would get surgery once you were clear of the problems again.